鱼C论坛

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

[已解决]关于结构体变量的问题

[复制链接]
发表于 2021-1-19 17:43:04 | 显示全部楼层 |阅读模式

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

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

x
前面这两种结构体类型指针定义是一样的吗,都是定义他们为首地址,如果不一样第二种定义是什么意思?如果一样为什么我最下面那段代码老是报错呢,求指教。
  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student *next;
  6. }stu,p1,p2;
  7. struct student *stu,*p1,*p2;
复制代码


  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student *next;
  6. }*stu,*p1,*p2;
复制代码



  1. #include <stdio.h>
  2. #if(1)
  3. struct object
  4.         {
  5.                 float Chinese;
  6.                 float Math;
  7.                 float English;
  8.         }obj;
  9. #endif
  10. struct student
  11.         {
  12.                 int num;
  13.                 char *name;
  14.                 struct object obj;
  15.         }*stu;
  16. int main(void)
  17. {
  18.         void print(struct student *p);
  19.         struct student *stu;
  20.         *stu={8,"JJ Lin",{98.5,90.0,95.5}};
  21.         print(stu);
  22. }

  23. void print(struct student *p)
  24. {
  25.         printf("num           :%d\n",(*p).num);
  26.         printf("name          :%s\n",(*p).name);
  27.         printf("Chinese score :%.1f\n",(*p).obj.Chinese);
  28.         printf("Math score    :%.1f\n",p->obj.Math);
  29.         printf("English score :%.1f\n",p->obj.English);
  30. }
复制代码
最佳答案
2021-1-19 18:05:36
本帖最后由 jackz007 于 2021-1-19 18:11 编辑
  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student * next ;
  6. }stu , p1 , p2                ;
  7. struct student * stu , * p1 , * p2 ; // 错误,相同的变量被重复定义
复制代码

  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student * next ;
  6. } * stu ,* p1 ,* p2           ; // 所有变量定义正确有效
复制代码

  1. #include <stdio.h>
  2. #if(1)
  3. struct object
  4.         {
  5.                 float Chinese     ;
  6.                 float Math        ;
  7.                 float English     ;
  8. } obj                             ;
  9. #endif
  10. struct student
  11.         {
  12.                 int num           ;
  13.                 char name[24]     ;  // name 需要实质性保存字符串,所以,不可以定义成指针
  14.                 struct object obj ;
  15. } * stu                           ;

  16. int main(void)
  17. {
  18.         void print(struct student * p)                     ;
  19.         struct student stu = {8,"JJ Lin",{98.5,90.0,95.5}} ; // stu 需要保存数据,不可以定义成指针,结构整体赋值只能在定义时进行
  20.         print(& stu)                                       ;
  21. }

  22. void print(struct student *p)
  23. {
  24.         printf("num           :%d\n",(*p).num);
  25.         printf("name          :%s\n",(*p).name);
  26.         printf("Chinese score :%.1f\n",(*p).obj.Chinese);
  27.         printf("Math score    :%.1f\n",p->obj.Math);
  28.         printf("English score :%.1f\n",p->obj.English);
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-19 18:05:36 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-1-19 18:11 编辑
  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student * next ;
  6. }stu , p1 , p2                ;
  7. struct student * stu , * p1 , * p2 ; // 错误,相同的变量被重复定义
复制代码

  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student * next ;
  6. } * stu ,* p1 ,* p2           ; // 所有变量定义正确有效
复制代码

  1. #include <stdio.h>
  2. #if(1)
  3. struct object
  4.         {
  5.                 float Chinese     ;
  6.                 float Math        ;
  7.                 float English     ;
  8. } obj                             ;
  9. #endif
  10. struct student
  11.         {
  12.                 int num           ;
  13.                 char name[24]     ;  // name 需要实质性保存字符串,所以,不可以定义成指针
  14.                 struct object obj ;
  15. } * stu                           ;

  16. int main(void)
  17. {
  18.         void print(struct student * p)                     ;
  19.         struct student stu = {8,"JJ Lin",{98.5,90.0,95.5}} ; // stu 需要保存数据,不可以定义成指针,结构整体赋值只能在定义时进行
  20.         print(& stu)                                       ;
  21. }

  22. void print(struct student *p)
  23. {
  24.         printf("num           :%d\n",(*p).num);
  25.         printf("name          :%s\n",(*p).name);
  26.         printf("Chinese score :%.1f\n",(*p).obj.Chinese);
  27.         printf("Math score    :%.1f\n",p->obj.Math);
  28.         printf("English score :%.1f\n",p->obj.English);
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-1-20 09:05:49 | 显示全部楼层

大佬,再问一下,、那下面这两个是一个意思吗,结构体变量名不是表示地址吗,那定义的结构体指针变量的变量名表示啥?
  1. struct student
  2. {
  3.         int num;
  4.         float score;
  5.         struct student * next ;
  6. }stu , p1 , p2  
复制代码

struct student
{
        int num;
        float score;
        struct student * next ;
} * stu ,* p1 ,* p2
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-2 12:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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