- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
由于包含可变长度数组,在 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/
我正在按照 PCL 文档的教程计算 2D 凸包 see here . 我有一片云和一些指数,将它们投影到具有给定系数的平面上,然后计算凸包。这是代码: PointCloud::Ptr tmpInlie
我想使用模拟退火在某个预定义的区间内找到单变量多项式函数的局部最小值。我也想尝试找到二次函数的全局最小值。 像这样的无导数算法不是解决问题的最佳方法,因此仅供学习。 虽然算法本身非常简单,但我不确定如
我正在寻找任意值的 2 维和 3 维索引的快捷方式,我知道这适用于给定数组 a[] #define a(i,j,k) a[(i)*span*span+(j)*span+(k)] #define b(i
我有一个形状为 [12, 8, 5, 5] 的 numpy 数组。我想修改每个元素的第 3 维和第 4 维的值。 例如 import numpy as np x = np.zeros((12, 80,
我是一名优秀的程序员,十分优秀!