c语言:为结构体指针的指针开辟空间

如:
struct stu
{
int *a;
}

main()
{
int i=0,j=0;
struct stu *p;
p=malloc(10*sizeof(p)); //为p开辟空间,成功
p->a=malloc(10*sizeof(int)); //不知道成功没
for(i=0;i<10;i++)
for(j=0;j<10;j--)
scanf("%d",&(info+i)->(a+j)); // error C2059: syntax error : '('

}

第1个回答  2011-11-29
描述详细一点追问

刚补充提问了,希望您看下

追答

malloc前面要加强制类型转换。
for(j=0;ja=malloc(10*sizeof(int)); 仅仅为p[0]的a分配了内存空间。你应该在for循环中为每个p[i]的a分配空间。
&(info+i)->(a+j);使用方法错了,应该是&p[i].a[j]

#include "stdio.h"
#include "stdlib.h"

struct stu
{
int *a;
}

main()
{
int i=0,j=0;
struct stu *p;
p=(struct stu *)malloc(10*sizeof(p)); //为p开辟空间,成功
for(i=0;i<10;i++)
{
p[i].a=(int *)malloc(10*sizeof(int)); //不知道成功没
for(j=0;j<10;j++)
scanf("%d",&p[i].a[j]); // error C2059: syntax error : '('
}

}

本回答被提问者采纳
相似回答