鱼C论坛

 找回密码
 立即注册
查看: 3498|回复: 8

incompatible types - from 'struct stuednt *' to 'struct student *'

[复制链接]
发表于 2014-2-12 16:42:10 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 Ъγ:_小ツ雨oο 于 2014-2-12 16:43 编辑
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define LEN sizeof(struct stuednt)
  5. int n;   //全局变量

  6. struct stuednt
  7. {
  8.         int num;
  9.         float score;
  10.         struct stuednt *next;
  11. };
  12. struct stuednt *creat();     //创建链表
  13. struct stuednt *del(struct stuednt *head, int num); // 传递的是 头和要删除的数
  14. void print(struct stuednt *head);  //打印出来

  15. void main()
  16. {
  17.         struct student *stu, *p;
  18.         int n;

  19.         stu = creat();
  20.         p = stu;
  21.         print(p);
  22.         printf("Please enter the delete num : ");
  23.         scanf("%d", &n);
  24.         print(del(p, n));

  25.         printf("\n\n");
  26.         system("pause");
  27. }

  28. struct stuednt creat()
  29. {
  30.         struct stuednt *head, *p1, *p2;

  31.         p1 = p2 = (struct stuednt *)malloc(LEN);

  32.         printf("Please enter the num : ");
  33.         scanf("%d", &p1->num);
  34.         printf("Please enter the score : ");
  35.         scanf("%f", &p1->score);

  36.         head = NULL;
  37.         n = 0;

  38.         while(p1->num)
  39.         {
  40.                 n++;
  41.                 if(n == 1)
  42.                 {
  43.                         head = p1;
  44.                 }
  45.                 else
  46.                 {
  47.                         p2->next = p1;
  48.                 }

  49.                 p2 = p1;
  50.                 p1 = (struct stuednt *)malloc(LEN);

  51.                 printf("Please enter the num :");
  52.             scanf("%d", &p1->num);
  53.             printf("Please enter the score :");
  54.             scanf("%f", &p1->score);
  55.         }

  56.         p2->next = NULL;
  57.         return head;
  58. }

  59. void print(struct stuednt *head)
  60. {
  61.         struct student *p;

  62.         p = head;

  63.         if(head)
  64.         {
  65.                 do
  66.                 {
  67.                         printf("%d号学生的成绩是: %5.1f\n", p->num, p->score);
  68.                         p = p->next;
  69.                 }while(p);
  70.         }
  71. }

  72. struct stuednt *del(struct stuednt *head, int num)
  73. {
  74.         struct stuednt *p1, *p2;

  75.         if(head == NULL)
  76.         {
  77.                 printf("在忽悠人!\n");
  78.                 goto end;
  79.         }

  80.         p1 = head;

  81.         while(p1->num != num && p1->next != NULL)
  82.         {
  83.                 p2 = p1;
  84.                 p1 = p1->next;
  85.         }
  86.         if(p1->num == num)
  87.         {
  88.                 if(head == p1)
  89.                 {
  90.                         head = p1->next;
  91.                 }
  92.                 else
  93.                 {
  94.                         p2->next = p1->next;
  95.                 }
  96.         printf("\nDelete No: %d succeed!\n",num);
  97.                 n = n-1;
  98.         }
  99.         else
  100.         {
  101.                 printf("%d not been found!\n", num);
  102.         }
  103. end :
  104.         return head;
  105. }
复制代码
求分析错误, 不晓得这个错误是什么意思,

最佳答案

查看完整内容

好吧,今天又巩固了一下结构体,以下程序编译成功
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-12 16:42:11 | 显示全部楼层
本帖最后由 oggplay 于 2014-2-21 00:02 编辑

好吧,今天又巩固了一下结构体,以下程序编译成功
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define LEN sizeof(struct stuednt)
  5. int n;   //全局变量

  6. struct stuednt
  7. {
  8.         int num;
  9.         float score;
  10.         struct stuednt *next;
  11. };
  12. struct stuednt *creat();     //创建链表
  13. struct stuednt *del(struct stuednt *head, int num); // 传递的是 头和要删除的数
  14. struct stuednt *print(struct stuednt *head);  //打印出来

  15. int main()
  16. {
  17.         struct student *stu, *p;
  18.         int n;

  19.       p=  creat();
  20.       
  21.         print(p);
  22.         printf("Please enter the delete num : ");
  23.         scanf("%d", &n);
  24.         print(del(p, n));//打印删除后的所有学生成绩

  25.       
  26.         system("pause");
  27.         return 1;
  28. }

  29. struct stuednt *creat()
  30. {
  31.         struct stuednt *head, *p1, *p2;

  32.         p1 = p2 = (struct stuednt *)malloc(LEN);

  33.         printf("Please enter the num : ");
  34.         scanf("%d", &p1->num);
  35.         printf("Please enter the score : ");
  36.         scanf("%f", &p1->score);

  37.         head = NULL;
  38.         n = 0;

  39.         while(p1->num)
  40.         {
  41.                 n++;
  42.                 if(n == 1)
  43.                 {
  44.                         head = p1;
  45.                 }
  46.                 else
  47.                 {
  48.                         p2->next = p1;
  49.                 }

  50.                 p2 = p1;
  51.                 p1 = (struct stuednt *)malloc(LEN);

  52.                 printf("Please enter the num :");
  53.             scanf("%d", &p1->num);
  54.             printf("Please enter the score :");
  55.             scanf("%f", &p1->score);
  56.         }

  57.         p2->next = NULL;
  58.         return head;
  59. }

  60. struct stuednt *print(struct stuednt *p)
  61. {
  62.   struct stuednt *p1;
  63.   p1=p;
  64.   while(p)
  65.     { printf("%d号学生的成绩是:%f\n\n",p->num,p->score);
  66.      p = p->next;}
  67.   p=p1;  return p;
  68. }

  69. struct stuednt  *del(struct stuednt *head, int num)
  70. {
  71.         struct stuednt *p1, *p2;

  72.         if(head == NULL)
  73.         {
  74.                 printf("在忽悠人!\n");
  75.                return head;
  76.         }

  77.         p1 = head;

  78.         while(p1->num != num && p1->next != NULL)
  79.         {
  80.                 p2 = p1;
  81.                 p1 = p1->next;
  82.         }
  83.         if(p1->num == num)
  84.         {
  85.                 if(head == p1)
  86.                 {
  87.                         head = p1->next;
  88.                 }
  89.                 else
  90.                 {
  91.                         p2->next = p1->next;
  92.                 }
  93.         printf("\nDelete No: %d succeed!\n",num);
  94.                 n = n-1;
  95.         }
  96.         else
  97.         {
  98.                 printf("%d not been found!\n", num);
  99.         }

  100.         return head;
  101. }
复制代码


2014-02-20 23:24:12 的屏幕截图.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-2-12 16:57:43 | 显示全部楼层
VC+ 6.0下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-12 19:14:50 | 显示全部楼层
看到你的代码目测一秒钟直接发现你有两个 n 变量一个是全局变量一个是局部变量.......
至于你说的目测是类型不符合 说白了你一个地方是student 一个地方是stuednt  其他的是否有错误不知道中......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-2-12 21:14:07 | 显示全部楼层

可是2个 n 是不一样的 啊, 一个是 传的值。   只是在小范围里面的 ,可能是错误的 把-- 我去调试调试去
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-12 21:40:47 | 显示全部楼层
Ъγ:_小ツ雨oο 发表于 2014-2-12 21:14
可是2个 n 是不一样的 啊, 一个是 传的值。   只是在小范围里面的 ,可能是错误的 把-- 我去调试调试去

如果这么写 全局变量会被局部变量覆盖掉的!这是语法规定
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-21 11:15:13 | 显示全部楼层
一点注释也没有,程序又这么长  都没兴趣看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-21 11:15:42 | 显示全部楼层
路过看看= =!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-2-27 17:08:52 | 显示全部楼层
有点长了,代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 12:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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