views:

71

answers:

4
#define STRLEN 65

/*Create linked list */
struct node {
   char str[STRLEN];
   struct node *next;
};

newInput = malloc(sizeof(struct node));
strcpy(newStr, newInput->str);

I left the other parts of the code, but it doesnt seem to be copying the string into newInput->str.

The string accepted is only 64 bytes.

It's just blank when I print it out after the copy. Any clue why?

+3  A: 

You have the arguments to strcpy reversed, the first argument is the destination, the second argument is the source. Try:

strcpy(newInput->str, newStr);
Mark E
oh gosh. Thanks!
Matt
Or even better, `strncpy(newInput->str, newStr, STRLEN);`
aschepler
`strncpy` is the **wrong** function to use here. It zero-fills the remainder of the size, which is a waste of time, and if `newStr` were longer than the destination size, it would not get null-terminated. **Never** use `strncpy` unless you really know what it was designed for.
R..
A: 

You have the arguments to strcpy reversed.

vanza
A: 

The first parameter of strcpy is the destination and the 2nd param is the source.

So

strcpy(newStr, newInput->str);

should be

strcpy(newInput->str,newStr);
codaddict
A: 

Your destination and source are backwards:

strcpy(newStr, newInput->str);

should be

strcpy(newInput->str, newStr);
JoshD