c语言struct 如何分配空间?
#include"stdio.h"structstu{intnote;intnum[3];};intmain(){structstu*peter;peter=(struc...
#include "stdio.h"
struct stu{
int note;
int num[3];
};
int main(){
struct stu* peter;
peter = (struct stu*)malloc(sizeof(struct stu*));
peter->a=100;
peter->num[0]=1;
peter->num[1]=2;
peter->num[2]=3;
printf("%d\n",peter->num[2]);
return 0;
}
为何提示我段错误?而我不给peter指针分配空间,反而可以运行?
敲错了,不是 peter->a ,是peter->note 展开
struct stu{
int note;
int num[3];
};
int main(){
struct stu* peter;
peter = (struct stu*)malloc(sizeof(struct stu*));
peter->a=100;
peter->num[0]=1;
peter->num[1]=2;
peter->num[2]=3;
printf("%d\n",peter->num[2]);
return 0;
}
为何提示我段错误?而我不给peter指针分配空间,反而可以运行?
敲错了,不是 peter->a ,是peter->note 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询