Chu*_*rch 0 c arrays malloc input
我需要创建一个数组,其大小由用户输入决定,然后指向所述数组.所有阵列都将保持在500-600之间的随机数.我似乎无法正确使用malloc.我还是C的新手,所以非常感谢帮助.
int main(){
int size;
printf("Enter size of array");
scanf("%d", &size);
int array[size];
int *aPtr = (int *) malloc(sizeof(int) * array);
Run Code Online (Sandbox Code Playgroud)
你只需要:
int *aptr = malloc(sizeof(int) * size);
Run Code Online (Sandbox Code Playgroud)
然后你可以像数组一样访问它.
aptr[0] = 123;
Run Code Online (Sandbox Code Playgroud)