[英]Copying a string to a struct member
I can't figure out how to copy a string from inputString
to newNode->data
. 我无法弄清楚如何将字符串从inputString
复制到newNode->data
。
My struct looks like this: 我的结构看起来像这样:
typedef struct node {
char *data;
struct node *left;
struct node *right;
} node;
And the function in questions looks like this: 问题中的函数如下所示:
node* addToTree(char inputString[]) {
node *newNode;
if ((newNode = malloc(sizeof(node*))) == NULL) {
printf("Error: could not allocate memory");
exit(-1);
}
if ((newNode->data = malloc(strlen(inputString) + 1)) == NULL) {
printf("Error: could not allocate memory");
exit(-1);
}
/* This line of code doesn't seem to copy anything to newNode->data.
This is the way I believe should work, however I don't understand what the
problem with it is. I have tried strlcpy and strncpy as well. */
strcpy(newNode->data, inputString);
/* The line below here seems to work when I print the value
within the function, but some of the values are garbage when
I try to use them later on in the program. */
newNode->data = inputString;
newNode->left = NULL;
newNode->right = NULL;
printf("Input string: %s\n", inputString);
printf("New node data: %s\n", newNode->data);
return newNode;
}
Your sizeof(node*)
does not represent the size you need. 您的sizeof(node*)
不代表您需要的大小。
newnode = malloc(sizeof(node*)) // wrong
newnode = malloc(sizeof (node)) // correct
newnode = malloc(sizeof *newNode) // better
Why is sizeof *newNode
better? 为什么sizeof *newNode
更好?
Because it prevents accidental forgetting to update the code in two places if the type changes 因为它可以防止在类型更改时意外忘记更新两个地方的代码
struct node {
char *data;
struct node *next;
struct node *prev;
};
struct nodeEx {
char *data;
size_t len;
struct nodeEx *next;
struct nodeEx *prev;
};
struct nodeEx *newnode = malloc(sizeof (struct node)); // wrong
struct nodeEx *newnode = malloc(sizeof *newnode); // correct
The below line does not allocate the required amount of memory, it allocates memory equal to the size of a pointer to node
. 下面的行不分配所需的内存量,它分配的内存等于指向 node
的指针的大小。
if ((newNode = malloc(sizeof(node*))) == NULL)
So your strcpy
fails because there is no memory to copy into. 所以你的strcpy
失败了因为没有要复制的内存。
Change the above to: 将以上内容更改为:
if ((newNode = malloc(sizeof(node))) == NULL)
What happens after you do the following is undefined behavior because the memory representing inputString
can be overwritten, and that is why you get garbage values later on. 执行以下操作后会发生什么是未定义的行为,因为可以覆盖表示inputString
的内存,这就是您以后获取垃圾值的原因。
newNode->data = inputString;
You can see the top answer to this question for additional information. 您可以查看此问题的最佳答案以获取更多信息。
newNode->data = inputString;
is incorrect, it overrides the previously malloc
ed memory. 如果不正确,它会覆盖以前的malloc
内存。
if ((newNode->data = malloc(strlen(inputString) + 1)) == NULL) {
printf("Error: could not allocate memory");
exit(-1);
}
strcpy(newNode->data, inputString);
is enough to allocate memory and copy the string into it. 足以分配内存并将字符串复制到其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.