简体   繁体   English

C-将字符串复制到链接列表变量中

[英]C - Copy string into a linked list variable

#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. 我离开了代码的其他部分,但似乎没有将字符串复制到newInput-> str中。

The string accepted is only 64 bytes. 接受的字符串只有64个字节。

It's just blank when I print it out after the copy. 当我在复印后打印出来时,它只是空白。 Any clue why? 有什么线索吗?

You have the arguments to strcpy reversed, the first argument is the destination, the second argument is the source. 您将strcpy的参数颠倒了,第一个参数是目标,第二个参数是源。 Try: 尝试:

strcpy(newInput->str, newStr);

您将strcpy的参数颠倒了。

The first parameter of strcpy is the destination and the 2nd param is the source. strcpy的第一个参数是目标,第二个参数是源。

So 所以

strcpy(newStr, newInput->str);

should be 应该

strcpy(newInput->str,newStr);

Your destination and source are backwards: 您的目的地和来源是倒退的:

strcpy(newStr, newInput->str);

should be 应该

strcpy(newInput->str, newStr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM