鱼C论坛

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

结构体指针

[复制链接]
发表于 2016-6-2 14:10:16 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>

  2. struct stu
  3. {
  4. char name[20];
  5. long number;
  6. float score[4];
  7. } ;

  8. int main(void)
  9. {
  10.        
  11.        
  12. struct stu *p1, *p2 ;       
  13. p1->number = 100;
  14. p2->number = 200;

  15. printf("%d\n,%d\n",p1->number,p2->number);       

  16. while(1)
  17. {
  18.         ;
  19.         }
  20.        
  21. }
复制代码


报错:
  1. 30 [main] StrPtr 3300 _cygtls::handle_exceptions: Error while dumping state
  2. (probably corrupted stack)
  3. Segmentation fault (core dumped)
复制代码


请帮忙分析哪里有段错误
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-6-2 14:45:55 | 显示全部楼层
找到问题了,错误有2:
1-野指针错误!
2-没有给新的结构体分配内存。更改后的代码如下:
  1. #include <stdio.h>

  2. struct stu
  3. {
  4. char name[20];
  5. long number;
  6. float score[4];
  7. } ;

  8. int main(void)
  9. {
  10. struct stu *p1=NULL, *p2=NULL ;       

  11. p1 = (struct stu*)malloc(sizeof(struct stu));
  12. p2 = (struct stu*)malloc(sizeof(struct stu));
  13. p1->number = 100;
  14. p2->number = 200;

  15. printf("%d\n,%d\n",p1->number,p2->number);       

  16. while(1)
  17. {
  18.         ;
  19. }
  20.        
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 14:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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