1
#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?

4 Answers 4

4

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

strcpy(newInput->str, newStr);
Sign up to request clarification or add additional context in comments.

2 Comments

Or even better, strncpy(newInput->str, newStr, STRLEN);
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.
0

You have the arguments to strcpy reversed.

Comments

0

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);

Comments

0

Your destination and source are backwards:

strcpy(newStr, newInput->str);

should be

strcpy(newInput->str, newStr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.