c链表问题

lanmansize 2008-07-31 01:11:16
自己写了个链表,编译通过后,运行报内存错误(unix下)请教大家
int n=2,m;
struct type
{
char name[50+1];
struct type *next;
};

struct type *first,*p;
strcpy(first->name,"zhangfeng");
p=first;
for(m=0;m<n;m++)
{
first->next=(struct type *)malloc(100);
strcpy(first->next->name,"hanzb");
first=first->next;
}
printf("%s \n",p->name);
printf("%s \n",p->next->name);
free(p->next);
free(p);
...全文
75 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zh928315 2008-07-31
  • 打赏
  • 举报
回复
1楼正确
sdjinshiyu 2008-07-31
  • 打赏
  • 举报
回复
用没有初始化的指针是很危险的
realdragon2 2008-07-31
  • 打赏
  • 举报
回复
mark~
Thorwein 2008-07-31
  • 打赏
  • 举报
回复
一旦指针的出现。就该想着它指向何方。
LUCI-Z 2008-07-31
  • 打赏
  • 举报
回复
没分配地址,你的指针指到那里都没有。
建议看看基础知识。
  • 打赏
  • 举报
回复
up 1楼!~
初始化很重要地!~无论是结构体,还是数组
bargio_susie 2008-07-31
  • 打赏
  • 举报
回复
首先你first没初始化

struct type *first = (struct type *)malloc(sizeof(struct type));

69,381

社区成员

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

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