我的意思是,如果我有一个像
这样的嵌套for循环for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; i++)
{
printf("%d\n"___);
}
}
我会在空白处放些什么?如果我已宣布一个阵列,[i] [j]会不合法吗?
答案 0 :(得分:2)
根据你的问题,我不确定你到底发生了什么,所以我制作了一个带评论的最小C程序
我声明了一个int
数组,其第一维和第二维至少为10,因为您将i
和j
从0重复到9(包括)。这是为了在迭代时避免出界问题
数组的元素未在程序中初始化。运行它时,该程序可能会打印全零。它也可能打印出恰好在内存中的其他值(因为数组值未初始化)
最后我在for循环之外声明了i
和j
,以防这是您遇到的问题
#include <stdio.h>
int main(int argc, char** argv) {
// Declare an array
// Note that both dimensions are at least 10
// Also note that none of the values are initialized here
int myArray[10][10];
// Both i and j are declared here rather than in the for loop
// This is to avoid the following potential error:
// error: 'for' loop initial declarations are only allowed in C99 or C11 mode
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
// Note that the values this prints are uninitialized
printf("%d\n", myArray[i][j]);
}
}
return 0;
}
答案 1 :(得分:1)
你的问题真的不清楚。但据我所知,你有一些二维数组,你想打印数组的内容。
您必须将arrary定义为int arr[10][10]
,然后才能使用,
printf("%d\n", arr[i][j]);