Reputation: 33
Is there such a way to initialize a char array and then loop through them all and change the value? I'm trying to create a table like structure.
I just keep getting errors like "Cannot initialize a value of type 'char' with an lvalue of type 'const char [3]'.
I'm using Xcode 6.1.1 for development
int width = 25;
int height = 50;
char board [50][25] = {""}; // height x width
for (int i = 0; i < width; i++) {
for (int j = 0; i < height; i++) {
if (i == 0) {
board[i][j] = {"||"};
}
}
}
Upvotes: 1
Views: 15637
Reputation: 141648
The problem is with board[i][j] = {"||"};
. The string "||"
cannot be implicitly converted to a single character.
It's not clear what you are trying to do; each cell of the board is a char
, and ||
is two chars. Two doesn't go into one. Perhaps you meant:
board[i][j] = '|';
Also, your loop nesting is backwards (the height
loop should be the outer one). The indexing of an array is the same as its declaration, so for board[i][j]
to work when the declaration was char board[50][25]
, i
must be ranging from 0
to 49
.
An improvement would be:
int const width = 25;
int const height = 50;
char board[height][width] = {};
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++)
Upvotes: 3
Reputation: 609
Set the characters in the array with single quotes, not double. So:
board[i][j] = '|';
Also, notice that you can only put a single character in each position
Upvotes: 0