鱼C论坛

 找回密码
 立即注册
查看: 3548|回复: 4

C视频55链表的例子,求解决?

[复制链接]
发表于 2011-10-3 00:52:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>

  2. struct student
  3. {
  4. long num;
  5. char *name;
  6. double score;
  7. struct student *next;
  8. } a, b, c = {10098, "jame", 28, &b, 10112, "tom", 90, &c, 11176, "rose", 92, NULL};

  9. void main()
  10. {
  11. struct  student  *head;
  12. head = &a;

  13. while(head)
  14. {
  15. printf("%ld %s %3g\n", head->num, head->name, head->score);
  16. head = head->next;
  17. }
  18. }
复制代码
编译后,提示一个错误: error C2078: too many initializers(太多的初始化),怎么解决?

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-3 01:01:31 | 显示全部楼层
  1. #include <stdio.h>

  2. struct student
  3. {
  4. long num;
  5. char *name;
  6. double score;
  7. struct student *next;
  8. } a, b, c = {10098, "jame", 28, &b, 10112, "tom", 90, &c, 11176, "rose", 92, NULL};  //这是什么意思?这是写什么?错了吧,这个初始化只是单独给c的。难怪太多初始化。

复制代码


小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-10-3 01:16:53 | 显示全部楼层

你也太快了吧,我还想等明天呢!
那我改成这样也不行啊!

  1. struct student
  2. {
  3. long num;
  4. char *name;
  5. double score;
  6. struct student *next;
  7. } a = {10098, "jame", 28, &b}, b ={10112, "tom", 90, &c}, c={11176, "rose", 92, NULL};
复制代码

要怎么弄初始化才行?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-3 02:28:40 | 显示全部楼层
本帖最后由 Y.H 于 2011-10-3 02:30 编辑
  1. #include <stdio.h>
  2. struct student
  3. {
  4.         long num;
  5.         char *name;
  6.         double score;
  7.         struct student *next;
  8. }a, b, c;

  9. void main()
  10. {
  11.         struct  student  *head;
  12.         //可以用scanf ("%ld", a->num);形式赋值, 也可以用a->num = 1213;形式赋值。
  13.         head = a;
  14.         a.next = b;
  15.         b.next = c;
  16.         c.next = NULL
  17.         while(head)
  18.         {
  19.                 printf("%ld %s %3g\n", head->num, head->name, head->score);
  20.                 head = head->next;
  21.         }
  22. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-10-3 10:56:35 | 显示全部楼层
也就是说只能按照视频中的例子一样赋初值,那我就不纠结了,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-8 16:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表