gpt4 book ai didi

更改一维和二维可变长度数组的大小

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

由于包含可变长度数组,在 C99 和下一个版本中,以下程序可以运行。

#include <stdio.h>

int main(void)
{
int i, numFibs;
printf ("How many Fibonacci numbers do you want (between 1 and 75)? ");
scanf ("%i", &numFibs);
if (numFibs < 1 || numFibs > 75)
{
printf ("Bad number, sorry!\n");
return 1;
}
unsigned long long int Fibonacci[numFibs];
Fibonacci[0] = 0; // by definition
Fibonacci[1] = 1; // ditto
for ( i = 2; i < numFibs; ++i )
{
Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1];
}
for ( i = 0; i < numFibs; ++i )
{
printf ("%llu ", Fibonacci[i]);
printf ("\n");
}
return 0;
}

但是当我尝试实现以下程序时,它工作正常直到 a<=9,a>9 的任何值程序都不会在数组中使用超过 9 个元素。

为什么?

#include <stdio.h>

int main()
{
int a=0;
int arr[a];
printf("Enter number of rows: ");
scanf("%d", &a);
printf("\nNo of rows to be entered: %d\n", a);
for(int j=0; j<a; j++)
{
printf("Enter array element[%d]: ", j);
scanf("%d", &arr[j]);
}
for(int j=0; j<a; j++)
{
printf("Entered array element [%d]: %d", j, arr[j]);
printf("\n");
}
return 0;
}

下面是一个二维数组的示例,它在 b<=2 的值之前工作正常,但该程序以分段失败告终。为什么?

#include <stdio.h>

int main()
{
int b=0;
int arr[b][2];
printf("Enter number of rows: ");
scanf("%d", &b);
printf("No of rows to be entered: %d", b);
for(int i=0; i<b; i++)
{
for(int j=0; j<2; j++)
{
printf("Enter array element[%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("\n");
for(int i=0; i<b; i++)
{
for(int j=0; j<2; j++)
{
printf("Enter array element[%d][%d]: %d", i, j, arr[i][j]);
printf("\n");
}
}
return 0;
}

在上面的示例中,动态获取行和列是错误的,但是动态获取行是可行的。如果您动态获取列,它也会失败。

没有malloc()有没有其他方法可以解决这些问题?

机器上运行的 GCC 版本:-

C:\Users\gahlot>gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe Target: mingw32 Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls Thread model: win32 gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)

最佳答案

这并不像你想象的那样:

int a=0;
int arr[a];
printf("Enter number of rows: ");
scanf("%d", &a);

一旦用给定的大小定义了数组,即使该大小不是常量表达式,数组的大小也是固定的。大小依赖于a的当前值。创建大小为 0 的数组也是无效的。

这在 C 标准的第 6.7.6.2p5 节中有详细说明:

If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by * ; otherwise, each time it is evaluated it shall have a value greater than zero. The size of each instance of a variable length array type does not change during its lifetime.

在这种情况下,数组的大小由单个变量指定,但是大小可以由任何表达式确定。例如:

int f()
{
int a, b, c;
scanf("%d", &a);
b=3;
c=9;
return sqrt(a) * (b - c) + sin(b);
}

...

int arr[f()];

如果您像这样定义 arr,即使您可以更改大小,您希望这种情况如何发生?

至于为什么您的第二段和第三段代码有时有效有时无效,这意味着您正在调用 undefined behavior .在这两种情况下,这是由于创建了一个至少一维为 0 的数组并随后尝试使用它造成的。

关于更改一维和二维可变长度数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944758/

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