[英]global 2d array in C
I tried initialising a 2D global array in C like this int array[100][100] = {{500}};
我尝试在 C 中初始化一个二维全局数组,就像这个
int array[100][100] = {{500}};
But then when I access the array elements from my function, the array elements show up as zero instead of 500. How to fix this?但是当我从 function 访问数组元素时,数组元素显示为零而不是 500。如何解决这个问题?
int array[100][100] = {{500}};
initializes only array[0][0]
to 500
.仅将
array[0][0]
初始化为500
。 The rest of array elements are initialized to 0
.数组元素的 rest 被初始化为
0
。
Example:例子:
int main(void)
{
int array[10][10] = {{500}};
for(size_t row = 0; row < 10; row++)
{
for(size_t col = 0; col < 10; col++)
printf("%4d", array[row][col]);
printf("\n");
}
}
Result:结果:
500 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
how would i make all the elements 500 in the global array?
我如何将全局数组中的所有元素设为 500?
int array[100][100] = {
/*line 1 */ {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, },
/* 100 lines */
/* ... */
/*line 100 */ {500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, },};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.