I am learning how to use structure with linked list in c. So I was trying to keep inserting the node to first position. Also, each node contains two different kinds of values, one is char array and another one is integer. Once I insert a value, then print the current list out. I look over and over again but I can't find where gets something wrong because I set a printf() test in the insert function. I saw the new_node is what I currently insert.
我正在学习如何在c中使用带有链表的结构。所以我试图继续将节点插入第一个位置。此外,每个节点包含两种不同的值,一种是char数组,另一种是整数。插入一个值后,打印出当前列表。我一遍又一遍地看,但我找不到哪里出错了,因为我在insert函数中设置了printf()测试。我看到new_node是我目前插入的。
When I insert a 1 first and it prints out a/1. But I am so confusing when I insert another set such as b 2, it prints out
当我先插入1并打印出一个/ 1时。但是当我插入另一组如b 2时,我很困惑,它打印出来
b/2
B / 2
b/1
B / 1
which means the name of first node changed along with inserting a new node, but the numbers did not change. Can anyone see what happened here?
这意味着第一个节点的名称随着插入一个新节点而改变,但数字没有改变。谁能看到这里发生了什么?
struct node{
char *name;
int numbers;
struct node *next;
};
struct node *list = NULL;
void printList(struct node *node){
while (node != NULL){
printf("%s/%d\n", node->name, node->numbers);
node = node->next;
}
}
void insert(struct node **list, char *name, int numbers){
struct node * new_node = malloc(sizeof(struct node));
new_node->name = name;
new_node->numbers = numbers;
//printf("......%s %d.........\n", new_node->name, new_node->numbers);
new_node->next = *list;
*list = new_node;
}
int main(){
......code for scanf() are omitted......
insert(&list, name, numbers);
printList(list);
}
2 个解决方案
#1
1
Your problem is that your are pointing on same buffer for every node. Even without seeing the code you removed, I can guess you are reading the node names in same char array. You would better consider using strdup() as it was suggested:
您的问题是您指向每个节点的相同缓冲区。即使没有看到你删除的代码,我猜你正在读取相同char数组中的节点名称。您最好考虑使用strdup(),因为它建议:
new_node->name = strdup(name);
Make sure you free this pointer when you are destroying the node.
在销毁节点时,请确保释放此指针。
#2
1
In C, strings take the form of 'character arrays', that is - a series of char variables next to each other, followed by a null character.
在C中,字符串采用'字符数组'的形式,即 - 一系列彼此相邻的char变量,后跟一个空字符。
When you see something like
当你看到类似的东西
char* string = "foo";
in C, that is the declaration of a char*
, (a pointer to a variable of type char
) assigned the memory address of the first character in a string. In your insert function the line
在C中,这是char *的声明,(指向char类型的变量的指针)分配了字符串中第一个字符的内存地址。在你的插入功能行
new_node->name = name;
takes the memory address assigned to name
and stores it in your object. It does not store the string value itself in your object; that remains exactly where it was. If you then use scanf()
, passing in the same pointer, you will overwrite the original string, which the object you previously created is still pointing to.
获取分配给name的内存地址并将其存储在对象中。它不会将字符串值本身存储在您的对象中;这仍然是它的确切位置。如果然后使用scanf(),传入相同的指针,您将覆盖原始字符串,您之前创建的对象仍然指向该字符串。
#1
1
Your problem is that your are pointing on same buffer for every node. Even without seeing the code you removed, I can guess you are reading the node names in same char array. You would better consider using strdup() as it was suggested:
您的问题是您指向每个节点的相同缓冲区。即使没有看到你删除的代码,我猜你正在读取相同char数组中的节点名称。您最好考虑使用strdup(),因为它建议:
new_node->name = strdup(name);
Make sure you free this pointer when you are destroying the node.
在销毁节点时,请确保释放此指针。
#2
1
In C, strings take the form of 'character arrays', that is - a series of char variables next to each other, followed by a null character.
在C中,字符串采用'字符数组'的形式,即 - 一系列彼此相邻的char变量,后跟一个空字符。
When you see something like
当你看到类似的东西
char* string = "foo";
in C, that is the declaration of a char*
, (a pointer to a variable of type char
) assigned the memory address of the first character in a string. In your insert function the line
在C中,这是char *的声明,(指向char类型的变量的指针)分配了字符串中第一个字符的内存地址。在你的插入功能行
new_node->name = name;
takes the memory address assigned to name
and stores it in your object. It does not store the string value itself in your object; that remains exactly where it was. If you then use scanf()
, passing in the same pointer, you will overwrite the original string, which the object you previously created is still pointing to.
获取分配给name的内存地址并将其存储在对象中。它不会将字符串值本身存储在您的对象中;这仍然是它的确切位置。如果然后使用scanf(),传入相同的指针,您将覆盖原始字符串,您之前创建的对象仍然指向该字符串。