需要用到malloc
int n, *p;
printf("please input a number:");
scanf("%d", &n);
if(n > 0){*p = malloc(n*sizeof(int));
}
上面的代码就可以动态分配空间。p也可以当做数组使用 p[0]....p[n-1]
这样写就可以了:
p = (int *)malloc(n*sizeof(int));