1

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] = {"||"};
        }

    }
}
1
  • Each element in your 2D array is a single char. Commented Dec 30, 2014 at 0:12

2 Answers 2

3

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++) 
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.