我有一个名为info的链表,它存储了两个const char *变量。 这是插入节点函数:
void add_user(Node **head,const char *id, const char *password) {
Node *n = (Node *) malloc(sizeof(Node));
n->id = id;
n->password = password;
n->next = *head;
*head = n;
printf("%p--\n", &n); //This address happens to be same all the time
}
这是调用函数的循环:
while (until eof) {
add_user(&info->head,(const char*)id-i, (const char*)password-j);
printf("==%s\n", info->head->id);//I tried this and this prints out properly
}
printf("%s\n", info->head->next->id); //this prints out the concatenated value of this node and the previous one
然而,如果我尝试以这种方式打印:
Node *cur = info->head;
while (cur != NULL) {
puts(cur->id);
cur = cur->next;
}
输出看起来好像每个循环都覆盖了节点:
apple
bananaapple
cherrybananaapple
正如我在评论中所写,头部的地址永远不会改变,我不确定它是否应该是相同的。此外,当我尝试在循环中打印出元素时,它会打印出来。