我有4个不同的阵列:
array_1[2][2]
array_2[2][2]
array_3[2][2]
array_4[2][2]
现在我要打印出所有数组的第一行:
int x=0;
int i=0;
for ( j = 0; j < 2; j++)
{
printf("%c ", array_x[i][j]);
if (j == 1)
{
x++;
j=0;
}
}
使用变量x我想将array_1到达array_4,但这不起作用。这是什么语法?
答案 0 :(得分:4)
这正是数组要完成的目标。
所以不是让array_1[2][2]
到4,而是有一个数组,可能是这样的:
a[4][2][2]
,每个元素的第一个元素将由a[x][0][0]
答案 1 :(得分:2)
您应该将4个阵列合并为3D阵列:array [4] [2] [2]。 这样,您就可以在一个特定维度上循环
答案 2 :(得分:0)
// use the following code to print out the first line of each array.
// note:
// the rowLength definition is so, if later the array row length changes
// it only has to be updated in one place
// note:
// the parens around the defined value, this habit can eliminate
// several errors when using more complex expressions/macros
#define rowLength (2)
int j = 0;
for ( j = 0; j < rowLength; j++)
{
printf("%c ", array_1[0][j]);
}
for ( j = 0; j < rowLength; j++)
{
printf("%c ", array_2[0][j]);
}
for ( j = 0; j < rowLength; j++)
{
printf("%c ", array_3[0][j]);
}
for ( j = 0; j < rowLength; j++)
{
printf("%c ", array_4[0][j]);
}