gpt4 book ai didi

c - 如何在c中创建动态大小的数组?

转载 作者:行者123 更新时间:2023-11-30 21:11:24 25 4
gpt4 key购买 nike

如何创建动态数组?我尝试使用该代码:

#include <stdlib.h>
#include <stdio.h>
int main()
{
int size=0,*value,array[size];
printf("How many array elements do you need?");
scanf_s("%i",&size);
value = (int *)malloc(size*sizeof(int));
printf("Your array has now %i elements.Here are the contents:",size);
for(i=0;i<size;i++)
{
printf("%i",array[i]);
}
return 0;
}

我怎样才能意识到这一点?我的程序(Visual Studio 2013)出现错误。

最佳答案

#include <stdlib.h>
#include <stdio.h>

int main(){
int i, size, *value;
printf("How many array elements do you need?");
scanf_s("%i", &size);
value = (int *)malloc(size*sizeof(int));
printf("Your array has now %i elements.Here are the contents:\n", size);
for(i=0;i<size;i++){
printf("%i\n", value[i]);//uninitialize
}
return 0;
}

关于c - 如何在c中创建动态大小的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23239986/

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