鱼C论坛

 找回密码
 立即注册
查看: 2832|回复: 3

学了链表的一些思考,为什么不可以这样实现结构体输入和打印?

[复制链接]
发表于 2012-3-22 12:49:45 | 显示全部楼层 |阅读模式
1鱼币
本帖最后由 lyq19900123 于 2012-3-22 14:22 编辑

我认为理论上没什么错误,运行起来会出错!
问题已解决,感谢指点,两次同样的错误了,加上逻辑不够严密,需要长期努力!fight!!!

#include<stdio.h>

#define LEN 3

void main()
{
         struct student
         {
               int number;
               char name[10];
               int score;
               struct student *next;
          };
struct student massage[LEN];
struct student *p;
int i;
p=massage;
printf("please input massage:\n");

for(i=0;i<LEN;p++,i++)
   {
         p->number=i+1;
         printf("name:");
         scanf("%s",p->name);
         printf("\nscore:");
         scanf("%d",p->score);
         p->next=p+1;
    }

p=massage;
do
    {
        printf("%d\t%s\t%d\t",p->number,p->name,p->score);
        p=p->next;
    }
while(p);
}

最佳答案

查看完整内容

复制进去 执行看看 详细的都在注释里面
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-3-22 12:49:46 | 显示全部楼层
复制进去 执行看看
详细的都在注释里面

  1. #include<stdio.h>

  2. #define LEN 3
  3. struct student
  4. {
  5.     int number;
  6.     char name[10];
  7.     int score;
  8.     struct student *next;
  9. };
  10. void main()
  11. {

  12.     struct student massage[LEN];
  13.     struct student *p;
  14.     int i;
  15.     p=massage;
  16.     printf("please input massage:\n");

  17.    
  18.     for(i=0;i<LEN;p++)
  19.     {
  20.             p->number=i+1;
  21.             printf("name:");
  22.             scanf("%s",p->name);
  23.             printf("score:");
  24.             scanf("%d",&(p->score));    // p->score 是一个值,要取地址
  25.             if(++i==LEN)
  26.                 p->next = NULL;            //确保最后一个p->next 指向空值
  27.             else
  28.                 p->next=p+1;
  29.     }

  30.     p=massage;

  31.     do
  32.     {
  33.         printf("%d\t%s\t%d\n",p->number,p->name,p->score);
  34.         p = p -> next;
  35.     }
  36.     while(p);
  37. }
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-3-22 13:42:26 | 显示全部楼层
链表的存储结构没有 数组的连续性 !p+1 不知道会指向那个内存 !
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-3-22 13:54:37 | 显示全部楼层
同上
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-27 04:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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