这个程序有问题啊 大兵给解释解释
#include<stdio.h>
#include<stdlib.h>
struct person
{int number;
int nextp;
};
main()
{
int i,n;
scanf("%d",&n);
struct person *p=NULL;
p=(struct person*)malloc(sizeof(struct person)*n);
for(i=0;i<n;i++)
{
p[i].nextp=i
p[i].number=i;
}
for(i=0;i<n;i++)
printf("%3d",p[i].number);
free(p);
}
错误为
E:\VC6.0\22.c(11) : error C2143: syntax error : missing ';' before 'type'
E:\VC6.0\22.c(12) : error C2065: 'p' : undeclared identifier
E:\VC6.0\22.c(12) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct person *'
E:\VC6.0\22.c(15) : error C2109: subscript requires array or pointer type
E:\VC6.0\22.c(15) : error C2224: left of '.nextp' must have struct/union type
E:\VC6.0\22.c(16) : error C2146: syntax error : missing ';' before identifier 'p'
E:\VC6.0\22.c(16) : error C2109: subscript requires array or pointer type
E:\VC6.0\22.c(16) : error C2224: left of '.number' must have struct/union type
E:\VC6.0\22.c(19) : error C2109: subscript requires array or pointer type
E:\VC6.0\22.c(19) : error C2224: left of '.number' must have struct/union type
E:\VC6.0\22.c(20) : warning C4022: 'free' : pointer mismatch for actual parameter 1
1、可以在结构体中添加指针类成员变量,并在成员函数中实现动态数组的分配。
2、以下以一个仅实现整型动态数组,不包含其它功能的类为例做说明。
为什么我运行提示那么多错
追答少分号,表示这一句没结束,而是和后一句连起来解释,你自己想想这得出多少错。先把分号加上去,再编一次,看看还剩下什么。估计只有main函数要求有返回值的警告了。
你在开玩笑吗
追答你开辟的空间p没有变化,所以每次循环你都是给已经赋过值的数据重新赋值。。。。
p=(struct person*)malloc(sizeof(struct person));
我开辟了n个
追答开辟了n个连续的空间,但是p始终指向这段空间的首地址,从始至终都不变。这也是你出错的地方