鱼C论坛

 找回密码
 立即注册
查看: 2687|回复: 2

关于函数返回的问题

[复制链接]
发表于 2011-12-6 09:43:33 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 surongre 于 2011-12-8 09:03 编辑

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #define LEN sizeof(struct student)   //student结构体大小

  5. struct student *creat();             //创建链表函数
  6. struct student *del(struct student *head,int num);  //删除链表节点函数
  7. void print(struct student *head);    //打印链表函数

  8. struct student   //定义结构体
  9. {
  10.       int num;
  11.       float score;
  12.       struct student *next;
  13. };

  14. int n;  //全局变量,用来记录存放了多少个节点

  15. void main()
  16. {
  17.       int num;
  18.       struct student *stu,*p;   //定义一个指向student结构体的指针
  19.       
  20.       stu = creat();       //调用creat函数,创建链表,并把creat函数的返回值赋值给stu
  21.       p = stu;
  22.       print(stu);         //调用print函数输出,参数为stu
  23.       printf("Please input you want to delete node:");
  24.       scanf("%d",&num);  //获取输入节点信息
  25.       print(del(p,num));   //调用del函数进行链表节点删除
  26.       
  27.       printf("\n\n");    //打印2个回车
  28.       system("pause");  //暂停
  29. }


  30. struct student *creat()   //crate函数
  31. {
  32.       struct student *head;   //定义head指针
  33.       struct student *p1,*p2; //定义p1,p2指针
  34.       p1 = p2 = (struct student *)malloc(LEN);  //调用malloc函数向系统申请sizeof(struct student)个空间
  35.       //并通过强制转换赋值给p1和p2两个指针
  36.       printf("Please input num:");         //打印提示
  37.       scanf("%d",&p1->num);               //获取整型输入,并赋值给p1->num
  38.       printf("Pleaes input score:");     //打印提示
  39.       scanf("%f",&p1->score);           //获取浮点型输入,并赋值给p2->score
  40.       printf("\n");
  41.       
  42.       head = NULL;                     //把NULL赋值给head,代表空
  43.       
  44.       n = 0;                          //初始化节点计数器
  45.       
  46.       while(p1->num)                //如果p1->num不等于0,即为真,这执行循环体,否则退出
  47.       {
  48.             n++;                    //节点计数器加1
  49.             if(1 == n)             //如果节点计数器等于1,说明是第一个节点,赋值给head
  50.             {
  51.                   head = p1;       //赋值语句,把p1赋值给头结点
  52.                   
  53.             }
  54.             else                  //如果节点计数器不等于1
  55.             {
  56.                   p2->next = p1;    //则把p2->next指向p1
  57.             }
  58.             
  59.             p2 = p1;                  //把p1赋值给p2
  60.             p1 = (struct student *)malloc(LEN);   //向系统申请sizeof(struct student)个空间,赋值给p1
  61.             
  62.             printf("Please input num:");  //打印提示
  63.             scanf("%d",&p1->num);         //获取整型输入,并赋值给p1->num
  64.             printf("Please input score:"); //打印提示
  65.             scanf("%f",&p1->score);     //获取浮点型输入,并赋值给p1->score
  66.             printf("\n");
  67.       }
  68.       p2->next = NULL;  //结束循环,把p2->next置为NULL,代表链表建立结束
  69.       free(p1);        //释放p1指向的内存
  70.       return head;      //返回head
  71. }


  72. void print(struct student *head)  //定义打印函数
  73. {
  74.       struct student *p;   //定义一个指向student结构体的指针p
  75.       printf("\nThere are %d records\n",n);   //打印节点计数器的值
  76.       p = head;                               //把head赋值给p
  77.       if(NULL != head)                         //如果head不等于NULL,执行循环体,否则退出
  78.       {
  79.             do
  80.             {
  81.                   printf("学号为%d的成绩是%f\n",p->num,p->score);  //打印p->num和p->score的值
  82.                   printf("\n");
  83.                   p = p->next;                        //把p->next赋值给p,即p指向下一个节点
  84.             }while(p);                          //如果p不等于NULL,继续循环,否则退出
  85.       }
  86.       
  87. }


  88. struct student(struct student *head,int num)   //定义删除链表节点函数
  89. {
  90.       struct student *p1,*p2;             //定义指向student结构的p1,p2,head指针
  91.       p1 = head;
  92.       p2 = p1->next;
  93.       if(NULL == head)
  94.       {
  95.             printf("Error:This linked list is empty!!!\n");
  96.             goto END;
  97.       }
  98.       else
  99.       {
  100.             while(p1->num != num && p1->next != NULL)
  101.             {
  102.                   p2 = p1;
  103.                   p1 = p1->next;
  104.             }
  105.             if(p1->num == num)
  106.             {
  107.                   if(p1 == head)
  108.                   {
  109.                         head = p1->next;
  110.                   }
  111.                   else
  112.                   {
  113.                         p2->next = p1->next;
  114.                   }
  115.                   printf("Delete node success\n");
  116.                   n = n-1;
  117.             }
  118.             else
  119.             {
  120.                   printf("Error: Not Found %d node",num);
  121.                   goto END;
  122.             }
  123.       }
  124. END:
  125.       return head;      //这里出问题了
  126. }

复制代码
编译时提示 error C2440: 'return' : cannot convert from 'struct student *' to 'int'error C2617: 'student' : inconsistent return statement


为什么会这样?我前面定义del函数是struct student *,和创建链表的函数定义是一样的,但是创建链表函数返回head则正常。。。。去掉删除链表节点的函数,正常编译正常运行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-6 10:18:32 | 显示全部楼层
lz这个代码我没怎么看,我只看了一下你标明出问题的地方.话说lz定义的那个函数98行struct student(struct student *head,int num)   //定义删除链表节点函数 这个问题lz没发现吗.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-6 11:05:51 | 显示全部楼层

原来忘记写函数名了,我还纳闷呢,怎么定义了struct然后说不能返回int型
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-3-29 23:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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