用malloc()和realloc()可以完成。
//---------------------------------------------------------------------------
#include
#include
#include
typedef struct
{
int i;
float a;
} std;
int main(void)
{
std **st;
int c=0;
//分配第一个结构体变量空间并为其赋值
st=(std **)malloc(sizeof(std *));
*st=(std *)malloc(sizeof(std));
scanf("%d%f",&(st[0]->i),&((st[0])->a));
//是否继续读入?
printf("Continue?(Y/N)");
getchar();
//如果按下y则表示要继续读入数据,这个while循环 处理这个过程,tolower()用于将字母转换为小写形式,此调用与动态分配无关。
while (tolower(getchar())=='y')
{
//为st重新分配空间以便容纳下一个结构体
st=(std **)realloc(st,sizeof(std *)*(++c+1));
st[c]=(std *)malloc(sizeof(std));
//为新的结构体读入数据
scanf("%d%f",&((st[c])->i),&((st[c])->a));
//询问是否继续读入
printf("Continue?(Y/N)");
getchar();
}
putchar('\n');
//读取过程结束,下面输出刚才读入结构体的数据
for (; c>=0; --c) {
printf("%d %g\n",st[c]->i,st[c]->a);
}
//释放为st分配的空间
free(st);
//屏幕暂停,以便观察结果
system("pause");
return 0;
}
//---------------------------------------------------------------------------
有什么问题?请指明。
此程序在VC++6.0和BCB2007中测试通过。
这个可以动态开辟的呀,用malloc()函数就可以了
malloc()动态分配内存空间.数组必须先定义后引用 一旦定义内存空间就不可以修改了....有时候会很浪费...