鱼C论坛

 找回密码
 立即注册
查看: 2197|回复: 1

结构体实参与形参问题

[复制链接]
发表于 2016-8-5 11:01:29 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 工科男 于 2016-8-5 11:13 编辑

代码是小甲鱼视频的有关结构体链表插入的案例。代码有些长哈,代码本身也没有错误。
大家可以跳到文末,看一下我的问题,再看代码。
  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);  //del函数用于删除结点, *head即链表
  7.                                                      //的头指针, num是要删除的结点num。
  8. struct student *insert(struct student *head, struct student *stu_2);  // 第一个参数需要被插入的链表
  9.                                                                       // 第二个参数待插入的结构的地址

  10. void print(struct student *head);   //打印链表

  11. struct student
  12. {
  13.       int num;
  14.       float score;
  15.       struct student *next;
  16. };

  17. int n; //全局变量,用来记录存放了多少数据。

  18. void main()
  19. {
  20.       struct student *stu, *p, stu_2;
  21.       int n;
  22.       
  23.       stu = creat();
  24.       p = stu;
  25.       print( p );
  26.       
  27.       printf("\nPlease input the num to delete: ");
  28.       scanf("%d", &n);
  29.       print( del(p, n) );

  30.       printf("\nPlease input the num to insert: ");
  31.       scanf("%d", &stu_2.num);
  32.       printf("Please input the score: ");
  33.       scanf("%f", &stu_2.score);

  34.       p = insert(stu, &stu_2);
  35.       print( p );


  36.       

  37.       printf("\n\n");
  38.       system("pause");
  39. }

  40. struct student *creat()
  41. {
  42.       struct student *head;
  43.       struct student *p1, *p2;
  44.       
  45.       p1 = p2 = (struct student *)malloc(LEN);  // LEN是student结构的大小
  46.       
  47.       printf("Please enter the num :");
  48.       scanf("%d", &p1->num);
  49.       printf("Please enter the score :");
  50.       scanf("%f", &p1->score);
  51.       
  52.       head = NULL;     
  53.       n = 0;   
  54.       
  55.       while( p1->num )
  56.       {
  57.             n++;
  58.             if( 1 == n )
  59.             {
  60.                   head = p1;                 
  61.             }
  62.             else
  63.             {
  64.                   p2->next = p1;
  65.             }
  66.             
  67.             p2 = p1;
  68.             p1 = (struct student *)malloc(LEN);
  69.             
  70.             printf("\nPlease enter the num :");
  71.             scanf("%d", &p1->num);
  72.             printf("Please enter the score :");
  73.             scanf("%f", &p1->score);
  74.       }
  75.       
  76.       p2->next = NULL;
  77.       
  78.       return head;
  79. }

  80. void print(struct student *head)
  81. {
  82.       struct student *p;
  83.       printf("\nThere are %d records!\n\n", n);
  84.       
  85.       p = head;
  86.       if( head )
  87.       {
  88.             do
  89.             {
  90.                   printf("学号为 %d 的成绩是: %f\n", p->num, p->score);
  91.                   p = p->next;
  92.             }while( p );
  93.       }
  94. }

  95. struct student *del( struct student *head, int num)
  96. {
  97.       struct student *p1, *p2;
  98.       
  99.       if( NULL == head )
  100.       {
  101.             printf("\nThis list is null!\n");
  102.             goto end;
  103.       }
  104.       
  105.       p1 = head;
  106.       while( p1->num != num && p1->next != NULL)
  107.       {
  108.             p2 = p1;
  109.             p1 = p1->next;
  110.       }
  111.       if( num == p1->num )
  112.       {
  113.             if( p1 == head )
  114.             {
  115.                   head = p1->next;
  116.             }
  117.             else
  118.             {
  119.                   p2->next = p1->next;
  120.             }
  121.             
  122.             printf("Delete No: %d succeed!\n", num);
  123.             n = n-1;
  124.       }
  125.       else
  126.       {
  127.             printf("%d not been found!\n", num);
  128.       }
  129.       
  130. end:
  131.       return head;
  132. }

  133. struct student *insert(struct student *head, struct student *stu_2)
  134. {
  135.       struct student *p0, *p1, *p2;
  136.       
  137.       p1 = head;
  138.       p0 = stu_2;
  139.       if( NULL == head )
  140.       {
  141.             head = p0;
  142.             p0->next = NULL;
  143.       }
  144.       else
  145.       {
  146.             while( (p0->num > p1->num) && (p1->next != NULL) ) //两种情况推出while,一:
  147.             {
  148.                   p2 = p1;
  149.                   p1 = p1->next;
  150.             }
  151.             
  152.             if( p0->num <= p1->num )
  153.             {
  154.                   if( head == p1 )   // p1是头结点,插入头部
  155.                   {
  156.                         head = p0;  
  157.                   }
  158.                   else               // 普通情况,插入中间
  159.                   {
  160.                         p2->next = p0;
  161.                   }

  162.                   p0->next = p1;
  163.             }
  164.             else   // p0的num最大,插入到末尾
  165.             {
  166.                   p1->next = p0;
  167.                   p0->next = NULL;
  168.             }
  169.       }

  170.       n = n+1;   // 由于插入了,所以增加了一位数据成员进入链表中。
  171.       
  172.       return head;
  173. }
复制代码


程序本身没有任何毛病哈。
我现在不大理解的是
请大家看代码第10行定义的子函数
struct student *insert(struct student *head, struct student *stu_2)
它的形参是两个结构体指针。
再看一下第26行
void main()
{
      struct student *stu, *p, stu_2;
      int n;
.
.
.
.
在主函数定义过程中,stu_2是结构体。

那么现在问题来了,子函数的形参明明是两个结构体指针变量,而实参有一个却是结构体变量。
按照我的理解,
形参与实参的数据类型应该相同啊,问题出在哪里了?
求解

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-5 15:29:36 | 显示全部楼层
没有关系的你仔细看他主函数里面写的  ,传参的时候是 &stu_2,看到了吗!!!!类型是一致的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-13 21:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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