gpt4 book ai didi

c - 在C中动态分配指定大小的数组

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:20 25 4
gpt4 key购买 nike

我需要创建一个数组,其大小由用户输入决定,然后有指向该数组的指针。所有数组将包含 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);

最佳答案

你只需要:

int *aptr = malloc(sizeof(int) * size);

然后您可以像访问数组一样访问它。

aptr[0] = 123;

关于c - 在C中动态分配指定大小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12556127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com