鱼C论坛

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

[已解决]结构体指针赋值的问题

[复制链接]
发表于 2021-1-25 11:24:06 | 显示全部楼层 |阅读模式

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

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

x
如下代码:
执行后无法输出,但是把指针赋值代码屏蔽完全用数组就可以了,请教问题出在哪里?

  1. #include <stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>


  4. struct Book
  5. {
  6.     char name[10];
  7.     int num;
  8. }class1[2];

  9. int main() {
  10.         struct Book *pa;
  11.         pa = (struct Book *)malloc(sizeof(struct Book));
  12.        
  13.         int i;
  14.         for(i=0; i<2; i++)
  15.         {
  16.                 char str[10];
  17.                 gets(str);
  18.                 strcpy(class1[i].name,str);
  19.                 strcpy(pa->name,str);
  20.                
  21.                 int number;
  22.                 scanf("%d",&number);
  23.                 getchar();
  24.                 pa->num = number;
  25.                 class1[i].num = number;
  26.                 pa ++;
  27.         }
  28.        
  29.         //pa = pb;
  30.        
  31.         for (i = 0;i< 2;i++)
  32.         {
  33.                 //printf("name is %s,num is %d\n",pa->name,pa->num);
  34.                 printf("name is %s,num is %d\n",class1[i].name,class1[i].num);
  35.                 //pa ++;
  36.         }
  37.        
  38.         return 0;
  39. }
复制代码
最佳答案
2021-1-25 11:41:22
本帖最后由 小甲鱼的铁粉 于 2021-1-25 11:42 编辑

有两处错误,第一处是定义时
  1. pa = (struct Book *)malloc(sizeof(struct Book));
复制代码

这里应该是两个内存空间,2 * sizeof(struct Book)
第二处是输入之后,pa++执行了两次,要让它回去之前的地址,就要 pa -= 2
正确代码如下
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include <stdio.h>
  5. #include<string.h>
  6. #include <stdlib.h>


  7. struct Book
  8. {
  9.     char name[10];
  10.     int num;
  11. }class1[2];

  12. int main() {
  13.         struct Book *pa;
  14.         pa = (struct Book *)malloc(2*sizeof(struct Book));//做了修改

  15.         int i;
  16.         for(i=0; i<2; i++)
  17.         {
  18.                 char str[10];
  19.                 gets(str);
  20.                 strcpy(class1[i].name,str);
  21.                 strcpy(pa->name,str);
  22.                
  23.                 int number;
  24.                 scanf("%d",&number);
  25.                 getchar();
  26.                 pa->num = number;
  27.                 class1[i].num = number;
  28.                 pa ++;
  29.         }
  30.         pa -= 2;//做了修改
  31.         //pa = pb;
  32.         
  33.         for (i = 0;i< 2;i++)
  34.         {
  35.                 printf("name is %s,num is %d\n",pa->name,pa->num);
  36.                 printf("name is %s,num is %d\n",class1[i].name,class1[i].num);
  37.                 pa ++;
  38.         }
  39.         system("pause");
  40.         return 0;
  41. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-25 11:41:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 小甲鱼的铁粉 于 2021-1-25 11:42 编辑

有两处错误,第一处是定义时
  1. pa = (struct Book *)malloc(sizeof(struct Book));
复制代码

这里应该是两个内存空间,2 * sizeof(struct Book)
第二处是输入之后,pa++执行了两次,要让它回去之前的地址,就要 pa -= 2
正确代码如下
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include <stdio.h>
  5. #include<string.h>
  6. #include <stdlib.h>


  7. struct Book
  8. {
  9.     char name[10];
  10.     int num;
  11. }class1[2];

  12. int main() {
  13.         struct Book *pa;
  14.         pa = (struct Book *)malloc(2*sizeof(struct Book));//做了修改

  15.         int i;
  16.         for(i=0; i<2; i++)
  17.         {
  18.                 char str[10];
  19.                 gets(str);
  20.                 strcpy(class1[i].name,str);
  21.                 strcpy(pa->name,str);
  22.                
  23.                 int number;
  24.                 scanf("%d",&number);
  25.                 getchar();
  26.                 pa->num = number;
  27.                 class1[i].num = number;
  28.                 pa ++;
  29.         }
  30.         pa -= 2;//做了修改
  31.         //pa = pb;
  32.         
  33.         for (i = 0;i< 2;i++)
  34.         {
  35.                 printf("name is %s,num is %d\n",pa->name,pa->num);
  36.                 printf("name is %s,num is %d\n",class1[i].name,class1[i].num);
  37.                 pa ++;
  38.         }
  39.         system("pause");
  40.         return 0;
  41. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-25 11:56:55 | 显示全部楼层
小甲鱼的铁粉 发表于 2021-1-25 11:41
有两处错误,第一处是定义时
这里应该是两个内存空间,2 * sizeof(struct Book)
第二处是输入之后,pa++ ...

感谢指点,追加2个问题:

1、这段代码我放在main函数外面定义就报错了
  1. struct Book *pa;
  2. pa = (struct Book *)malloc(2*sizeof(struct Book));
复制代码


2、这样定义也报错
  1. struct Book
  2. {
  3.     char name[10];
  4.     int num;
  5. }class1[2],*pa=&class1;
复制代码


初学基础知识不扎实,请指点敏捷
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-25 14:57:10 | 显示全部楼层
dysow 发表于 2021-1-25 11:56
感谢指点,追加2个问题:

1、这段代码我放在main函数外面定义就报错了

放在main函数外面就是全局变量,定义时就要初始化,分配好内存空间
  1. struct Book
  2. {
  3.     char name[10];
  4.     int num;
  5. }class1[2];

  6. struct Book *pa = (struct Book *)malloc(2*sizeof(struct Book));

  7. int main() {
  8.     //省略
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-2 02:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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