链表问题,请大家帮忙解答,我想知道我的为什么错误?

tracera 2011-09-01 10:29:49
换了家新单位,不能上网,刚去没有什么事情,就想写个链表吧。假如将头指针作为参数应该很简单。(呵呵,有机会再试试)。假如像我现在这样使用全局指针,得不到正确的结果啊。
p->id=4,
p->next is not null
p->id=4,
p->next is null
this is a test for test4
以上是结果,明显是不对。为什么呢原因在哪里,请指教谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int id;
// char name[20];
struct node *next;
};

struct node *init_node(struct node ptr);
struct node *head;
void add_node(struct node ptr);
void show_allnode(void);


int main(void)
{
struct node ptr;
ptr.id = 1;
ptr.next = NULL;
head = init_node(ptr);
ptr.id = 2;
ptr.next = NULL;
add_node(ptr);
ptr.id = 3;
ptr.next = NULL;
add_node(ptr);
ptr.id = 4;
ptr.next = NULL;
add_node(ptr);
show_allnode();
printf(" this is a test for test4\n");
return 0;
}

void show_allnode(void)
{
struct node *p;
p = head;
if (p == NULL)
{
printf("head is null\n");
}
printf(" this is a test for show all node\n");
while(p != NULL)
{
printf("p->id=%d,\n", p->id);
if (p->next == NULL)
{
printf("p->next is null\n");
}
else
{
printf("p->next is not null\n");
}
p = p->next;
}
}


struct node *init_node(struct node ptr)
{
struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (headi1 == NULL)
{
printf("malloc error\n");
}
head1 = &ptr;
head = head1;
return head;

}



void add_node(struct node ptr)
{
struct node *p;
p = head;
if (p == NULL)
{
printf("head is null\n");
}
while(p->next != NULL)
{
p = p->next;
}

struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (head1 == NULL)
{
printf("malloc error\n");
}
head1->id = ptr.id;
p->next = head1;
head1->next = NULL;

}
...全文
72 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
EmbeddedLong 2011-09-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhao4zhong1 的回复:]

内存地址→指针→链表
VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习汇编以及C和汇编的对应关系。
从汇编的角……
[/Quote]

复制帝 出现
諾不輕許 2011-09-02
  • 打赏
  • 举报
回复

struct node *init_node(struct node ptr)
{
struct node *head1 = (struct node *) malloc(sizeof(struct node) * 1);
if (head1 == NULL)
{
printf("%s(%d): malloc error\n", __FILE__, __LINE__);
return NULL;
}
memcpy(head1, &ptr, sizeof(struct node));
//head1 = &ptr;
head = head1;
return head;
}
改成这样,试试...
赵4老师 2011-09-02
  • 打赏
  • 举报
回复
内存地址→指针→链表
VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习汇编以及C和汇编的对应关系。
从汇编的角度理解和学习C语言的指针,原本看似复杂的东西就会变得非常简单!
指针即地址。“地址又是啥?”“只能从汇编语言和计算机组成原理的角度去解释了。”

提醒:
“学习用汇编语言写程序”

“VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
想要从本质上理解C指针,必须学习C和汇编的对应关系。”
不是一回事!
EmbeddedLong 2011-09-02
  • 打赏
  • 举报
回复
楼主居然可以顺利编译过 我编译的时候回程序 崩溃的 ,,,,
EmbeddedLong 2011-09-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kidlhm 的回复:]


struct node *init_node(struct node ptr)
{
struct node *head1 = (struct node *) malloc(sizeof(struct node) * 1);
if (head1 == NULL)
{
……
[/Quote]

代码在
 这里添加代码的    
tracera 2011-09-02
  • 打赏
  • 举报
回复
谢谢各位回复,我知道那里有问题了。大家也要注意
void add_node(struct node ptr)
p1 = &ptr;直接使用参数的地址,不对,应该这个是传值
p1->id = ptr.id;
[color=#00FF00]p1->next = NULL;
[/color]
可以说还是基础问题,函数参数是传值,还是传地址。
下面的编译没有问题(linux gcc编译成功)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 100
struct node{
int id;
struct node *next;
};

struct node *head;
struct node *init_node(struct node ptr);
void add_node (struct node ptr);
void show_all_node();
int numi = 0;

int main(void)
{
struct node p1;
int i = 0;
p1.id = 1;
p1.next = NULL;
int num = sizeof(struct node);
printf("test4 NUM = %d sizeof(struct node)=%d\n", NUM, num);
head = init_node(p1);
if(NULL == head)
{
printf("-init_node erro-\n");
}
show_all_node();
printf("-1-end add_node-\n");
p1.id = 2;
p1.next = NULL;
add_node(p1);
// show_all_node();
printf("-2-end add_node-\n");
p1.id = 3;
p1.next = NULL;
add_node(p1);
// show_all_node();
printf("-3-end add_node-\n");
for (i = 4; i < 20; i ++)
{
p1.id = i;
add_node(p1);
}
printf("-4-end add_node-\n");
show_all_node();
if (head == NULL)
{
printf("malloc error\n");
free(head);
head = NULL;
}
return 0;
}

void show_all_node()
{
struct node *p;
p = head;
int i = 0;
while(p != NULL)
{
i ++;
// printf("p 0x%x\n", p);
if (i > 5)
{
// break;
}
printf("%d show all node p->id=%d, ", i, p->id);
printf("p-next 0x%x\n", p->next);
p = p->next;
}
}


void add_node(struct node ptr)
{
struct node *p;
struct node *p1;
printf("-1-addmode\n");
if (head == NULL)
{
printf("head is NULL\n");
}
p = head;
printf("-2-addmode\n");
printf("-1-add_node-p-next 0x%x\n", p->next);
while (p->next != NULL)
{
// printf("-while-add_node-p-next 0x%x\n", p->next);
p = p->next;
// printf("-while-add_node-p-next 0x%x\n", p->next);
}

printf("-3-addmode\n");
printf("-2-add_node-p.trnext 0x%x\n", ptr.next);
p1 = &ptr;

int num = sizeof(struct node);
p1 = (struct node *)malloc(num * 1);
p1->id = ptr.id;
p1->next = NULL;
p->next = p1;
printf("-2-add_node-p-next 0x%x\n", p->next);
printf("-2-add_node-p1-next 0x%x\n", p1->next);
struct node *h;
if (p1 != NULL)
{
// free(p1);
p1 = NULL;
}
h = head;
int i = 1;
printf("-4-addmode\n");
}

struct node *init_node(struct node ptr)
{
int num = sizeof(struct node);
head = (struct node *)malloc(num * NUM);
if (head == NULL)
{
printf("malloc error\n");
return NULL;
}
memset(head, '\0', (num * NUM));
head->id = ptr.id;
head->next = ptr.next;
return head;
}
程序员小迷 2011-09-01
  • 打赏
  • 举报
回复
你好,我看了下代码,这里貌似有问题:
struct node *init_node(struct node ptr)
{
struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (headi1 == NULL)
{
printf("malloc error\n");
}
head1 = &ptr;
head = head1;
return head;

}
返回的head是局部变量的地址啊,这个地址使用会出错的哦。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧