C 如何让用户确定全局变量的数组大小?
我想尝试创建一个C程序,让用户确定全局变量的数组大小(使我的程序更具动态性)。但是有一个陷阱,因为您不能在范围之外使用C 如何让用户确定全局变量的数组大小?,c,arrays,dynamic,global,C,Arrays,Dynamic,Global,我想尝试创建一个C程序,让用户确定全局变量的数组大小(使我的程序更具动态性)。但是有一个陷阱,因为您不能在范围之外使用scanf。你们能帮我吗 我尝试在范围之外使用scanf,但这是不可能的 这是有效的代码(用户无法确定全局变量的数组大小): #包括 int-arr[100]; int tempArr[100]; 无效合并(整数左、整数中、整数右){ int leftIndex,rightIndex,index; 对于(leftIndex=left,rightIndex=middle+1,ind
scanf
。你们能帮我吗
我尝试在范围之外使用scanf
,但这是不可能的
这是有效的代码(用户无法确定全局变量的数组大小):
#包括
int-arr[100];
int tempArr[100];
无效合并(整数左、整数中、整数右){
int leftIndex,rightIndex,index;
对于(leftIndex=left,rightIndex=middle+1,index=left;leftIndex原始代码为:
#include <stdio.h>
int slot;
scanf("%d",&slot);
int arr[slot];
int tempArr[slot];
#包括
int槽;
scanf(“%d”和插槽);
int arr[插槽];
int tempArr[槽];
修改后的代码为:
#include <stdio.h>
#include <stdlib.h>
int slot;
scanf("%d",&slot);
int *arr=malloc(sizeof *arr *slot);
int *tempArr=malloc(sizeof *tempArr *slot);
#包括
#包括
int槽;
scanf(“%d”和插槽);
int*arr=malloc(sizeof*arr*slot);
int*tempArr=malloc(sizeof*tempArr*slot);
下面是一个关于“让用户确定全局变量的数组大小”的示例
#包括
#包括
//[1]。请使用“指针”而不是“数组”
int*arr;
int size=0;
void init(void)
{
int i;
对于(i=0;i
如何让用户确定全局变量的数组大小
用户不确定变量的大小,程序员是这样做的。例如,设置一个静态的、允许的最大大小可能非常有意义,然后一个有效的用例可能是只使用数组的一部分
如果需要在运行时设置数组的大小,可以使用动态分配和malloc
。研究动态分配。就是这样。声明为“全局”(在文件范围内)的数组不能在运行时设置其大小。必须使用可在编译时计算的常量表达式指定其大小(例如数字文字或另一个对象上的sizeof
操作)
具有static
存储持续时间的对象(全局对象和任何使用static
关键字声明的对象)在程序启动时,即在提示用户输入任何内容之前加载到内存中,并且必须立即知道其大小
因此,如果您想要一个直到运行时才知道大小的“全局”数组,则必须使用malloc
或calloc
:
int *arr = NULL;
int *tmpArr = NULL;
...
int main( void )
{
int arrsize = 0;
...
scanf( "%d", &arrsize ); // ideally want to check that scanf succeeded.
arr = malloc( sizeof *arr * arrsize );
if ( !arr )
{
// malloc failed, probably want to exit here
}
tempArr = malloc( sizeof *tempArr * arrsize );
if ( !tempArr )
{
// same as above
}
...
完成后,在退出程序之前,用free
释放内存:
...
free( arr );
free( tempArr );
exit( EXIT_SUCCESS );
}
给你一个词:malloc
。关于如何实现malloc
,我有相当多的了解。也许,一个快速的教程会有所帮助。谢谢你的快速回复。@JL2210我猜他的意思是“如何使用malloc
”。埃尔默:关于如何做到这一点,有无数的教程(到目前为止,这里有几个问题的答案。)我会尝试在网上搜索“C malloc dynamic array”或者诸如此类。如果重点是创建一个灵活的、可重用的库,那么你有没有考虑过全局变量在这种情况下有多大问题?@JohnBollinger,我会说,一步一步。在担心全局变量之前,我会担心让用户明确地预先指定一些程序可能不应该指定的东西o自己弄清楚,真正动态地。请包括你在代码之外更改的内容的详细信息。这是一个只针对代码的答案,这是一件坏事。详细说明和解释,而不仅仅是给出代码示例。此外,这个答案教人坏习惯。@klutt,这几乎就像一个如何不编写C的清单:忽略结果对于scanf()
,强制转换malloc()
的结果,不检查失败的malloc()
,声明了范围大于必要范围的无意义名称的变量,非原型main()
而不是main(void)
。不铸造malloc=击败死马的结果。我们从…99开始使用C99。自从malloc铸造+缺少的stdlib include bug过时以来已经20年了。铸造是相当无意义的,但如今也是无害的。至于忽略scanf的结果…主要错误首先是使用stdio.h。这是一辆马车,爸爸库的主要目的是在学术环境中学习C语言,在学术环境中,缓冲区溢出之类的事情是外围感兴趣的。
#include <stdio.h>
#include <stdlib.h>
// [1]. use "pointer" instead of "array"
int *arr;
int size=0;
void init(void)
{
int i;
for(i=0;i<size;i++)
{
arr[i]=i;
}
}
void display(void)
{
int i;
for(i=0;i<size;i++)
{
printf("%d, ",arr[i]);
}
printf("\n");
}
int main(void)
{
if(scanf("%d",&size)==-1)
{
return 0;
}
//[2]. use "malloc" to request variable size of memory
// "malloc" require <stdlib.h>
arr=malloc(sizeof *arr *size);
if(arr==NULL)
{
return 0;
}
//init and display
init();
display();
//[3]. releasing the memory space is a good habit.
free(arr);
return 0;
}
int *arr = NULL;
int *tmpArr = NULL;
...
int main( void )
{
int arrsize = 0;
...
scanf( "%d", &arrsize ); // ideally want to check that scanf succeeded.
arr = malloc( sizeof *arr * arrsize );
if ( !arr )
{
// malloc failed, probably want to exit here
}
tempArr = malloc( sizeof *tempArr * arrsize );
if ( !tempArr )
{
// same as above
}
...
...
free( arr );
free( tempArr );
exit( EXIT_SUCCESS );
}