So I'm quite a newbie, so please go soft.
我是个新手,所以请温柔点。
I have created a 2D array, 60x30 and want to display it on the screen as a grid, by doing a double for loop. I am using a simple character '.' for each slot of the grid just for a test.
我创建了一个2D数组,60x30,并希望通过双for循环在屏幕上以网格的形式显示它。我用的是一个简单的字符。“这是为了测试网格的每个槽。”
char FrameBuffer[29][59];
for (int i = 0; i <= 29; i++)
{
for (int j = 0; j <= 59; j++)
{
FrameBuffer[i, j] = '.';
printf("%c ", FrameBuffer[i,j]);
}
printf("\n");
}
However, whenever I try and assign a value to a position in my 2D array, e.g.
但是,每当我尝试为我的2D数组中的一个位置赋值时,例如。
FrameBuffer[0,1] = '.',
I am greeted with an error:
我犯了一个错误:
Expression must be a modifiable lvalue
3 个解决方案
#1
5
FrameBuffer[29][59]
This is a 29x59 array, you need a 30x60.
这是一个29x59数组,需要30x60。
Please don't mix up array declarations with array indexing. Array declarations are fully sane; if you need 30x60 then you type FrameBuffer[30][60]
. When you access the array however, you start at index 0.
请不要将数组声明与数组索引混淆。数组声明完全正常;如果需要30x60,则输入FrameBuffer[30][60]。然而,当您访问数组时,您从索引0开始。
Simply change the code to:
只需将代码更改为:
char FrameBuffer[30][60];
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 60; j++)
{
FrameBuffer[i][j] = '.'; // note the correct syntax here
EDIT
编辑
As a curious side-effect, FrameBuffer[i, j]
is interpreted as something entirely different than intended. The comma here is regarded as the comma operator, a special kind of operator which evaluates the left expression i
, then the right expression j
, then returns j
.
作为一个奇怪的副作用,FrameBuffer[i, j]被解释为完全不同于预期的东西。这里的逗号被视为逗号运算符,是一种特殊的运算符,它计算左表达式i,然后计算右表达式j,然后返回j。
Meaning that the code ended up completely equivalent to FrameBuffer[j] = '.'
. Where FrameBuffer[j]
is a whole array, not a char
. You can't assign a value to an array this way, an array is not a "lvalue", which explains the compiler error text.
这意味着代码最终完全等同于FrameBuffer[j] = '.'。其中FrameBuffer[j]是一个完整的数组,而不是字符。不能这样给数组赋值,数组不是“lvalue”,这解释了编译器错误文本。
#2
1
FrameBuffer[i,j] = '.';
FrameBuffer(i,j)= '。';
printf("%c ", FrameBuffer[i,j]);
printf(" % c”,FrameBuffer(i,j));
modify these two statements to
将这两个语句修改为
FrameBuffer[i][j] = '.';
printf("%c ", FrameBuffer[i][j]);
[i,j]
means nothing to the compiler. It's nothing but an invalid syntax.
[i,j]对编译器没有任何意义。它只是一个无效的语法。
also correct you array.
还正确的数组。
FrameBuffer[29][59]
it is an array of row=29
and col=59
FrameBuffer[29][59]它是一行=29和col=59的数组。
change it to FrameBuffer[60][30];
将其更改为FrameBuffer[60][30];
#3
-1
char FrameBuffer[31][61];
for (int i = 0; i <= 29; i++)
{
for (int j = 0; j <= 59; j++)
{
FrameBuffer[i][j] = '.';
printf("%c ", FrameBuffer[i][j]);
}
FrameBuffefr[i][j] = '\0';
printf("\n");
}
FrameBuffer[i] = NULL;
}
Here you assign 30*60 characters, so you need to allocate 30 * 60 + 1 on each to add a character to indicate the end ('\0'). (easy to parse it, because you don't need the size you can do a while(Framebuffer[i][j]) it will be a value 0 on the '\0').
在这里,您分配了30*60个字符,因此您需要为每个分配30*60 + 1以添加一个字符来指示结束('\0')。(解析起来很容易,因为您不需要一次性使用的大小(Framebuffer[i][j]))它在'\0'上的值为0)。
#1
5
FrameBuffer[29][59]
This is a 29x59 array, you need a 30x60.
这是一个29x59数组,需要30x60。
Please don't mix up array declarations with array indexing. Array declarations are fully sane; if you need 30x60 then you type FrameBuffer[30][60]
. When you access the array however, you start at index 0.
请不要将数组声明与数组索引混淆。数组声明完全正常;如果需要30x60,则输入FrameBuffer[30][60]。然而,当您访问数组时,您从索引0开始。
Simply change the code to:
只需将代码更改为:
char FrameBuffer[30][60];
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 60; j++)
{
FrameBuffer[i][j] = '.'; // note the correct syntax here
EDIT
编辑
As a curious side-effect, FrameBuffer[i, j]
is interpreted as something entirely different than intended. The comma here is regarded as the comma operator, a special kind of operator which evaluates the left expression i
, then the right expression j
, then returns j
.
作为一个奇怪的副作用,FrameBuffer[i, j]被解释为完全不同于预期的东西。这里的逗号被视为逗号运算符,是一种特殊的运算符,它计算左表达式i,然后计算右表达式j,然后返回j。
Meaning that the code ended up completely equivalent to FrameBuffer[j] = '.'
. Where FrameBuffer[j]
is a whole array, not a char
. You can't assign a value to an array this way, an array is not a "lvalue", which explains the compiler error text.
这意味着代码最终完全等同于FrameBuffer[j] = '.'。其中FrameBuffer[j]是一个完整的数组,而不是字符。不能这样给数组赋值,数组不是“lvalue”,这解释了编译器错误文本。
#2
1
FrameBuffer[i,j] = '.';
FrameBuffer(i,j)= '。';
printf("%c ", FrameBuffer[i,j]);
printf(" % c”,FrameBuffer(i,j));
modify these two statements to
将这两个语句修改为
FrameBuffer[i][j] = '.';
printf("%c ", FrameBuffer[i][j]);
[i,j]
means nothing to the compiler. It's nothing but an invalid syntax.
[i,j]对编译器没有任何意义。它只是一个无效的语法。
also correct you array.
还正确的数组。
FrameBuffer[29][59]
it is an array of row=29
and col=59
FrameBuffer[29][59]它是一行=29和col=59的数组。
change it to FrameBuffer[60][30];
将其更改为FrameBuffer[60][30];
#3
-1
char FrameBuffer[31][61];
for (int i = 0; i <= 29; i++)
{
for (int j = 0; j <= 59; j++)
{
FrameBuffer[i][j] = '.';
printf("%c ", FrameBuffer[i][j]);
}
FrameBuffefr[i][j] = '\0';
printf("\n");
}
FrameBuffer[i] = NULL;
}
Here you assign 30*60 characters, so you need to allocate 30 * 60 + 1 on each to add a character to indicate the end ('\0'). (easy to parse it, because you don't need the size you can do a while(Framebuffer[i][j]) it will be a value 0 on the '\0').
在这里,您分配了30*60个字符,因此您需要为每个分配30*60 + 1以添加一个字符来指示结束('\0')。(解析起来很容易,因为您不需要一次性使用的大小(Framebuffer[i][j]))它在'\0'上的值为0)。