Saurabh Khirwal
Saurabh Khirwal

Reputation: 341

Create a 2d character array in c

I want to create a 2d character array in C just like this one in php :

$matrix = array(
                array('$','@','*','%','&','#','@','#','#','@'),
                array('%','$','@','%','#','%','@','*','&','*'),
                array('#','*','&','*','#','@','@','&','%','@'),
                array('%','@','*','$','%','&','#','*','@','&'),
                array('$','*','&','&','&','@','#','%','*','*'),
                array('#','#','@','#','&','%','*','$','#','#'),
                array('&','$','$','#','@','#','@','$','%','*'),
                array('@','$','$','*','&','$','#','*','#','*'),
                array('%','$','*','@','&','@','&','#','#','#'),
                array('#','@','%','*','#','&','#','$','%','#')
            );

I tried writing up this code :

int size = 10;
char matrix[size][size] = {
                {'$','@','*','%','&','#','@','#','#','@'},
                {'%','$','@','%','#','%','@','*','&','*'},
                {'#','*','&','*','#','@','@','&','%','@'},
                {'%','@','*','$','%','&','#','*','@','&'},
                {'$','*','&','&','&','@','#','%','*','*'},
                {'#','#','@','#','&','%','*','$','#','#'},
                {'&','$','$','#','@','#','@','$','%','*'},
                {'@','$','$','*','&','$','#','*','#','*'},
                {'%','$','*','@','&','@','&','#','#','#'},
                {'#','@','%','*','#','&','#','$','%','#'}
            };

I am very new to c so i don't really know the concept of 2d arrays in c. But for some reason the above code is giving error. Please help.

Upvotes: 2

Views: 3350

Answers (4)

Ivan  Ivanov
Ivan Ivanov

Reputation: 2106

  1. In C99 there is a variable length array, so you can do that, but not all compilers support vla and C99 standard (@Jonathan Leffler but still can not initialize array).
  2. So the solution is to give computer information about size of array before compilation. So you can define size with macros

    #define size 10
    
  3. Or to create array dynamically on heap using malloc (and other memory allocating functions)

  4. Or to create array dynamically on stack using alloca and malloca
  5. Also on some compilers I found that const variables can be used to do that, but that is rare (found on borland) and not standard.
  6. If you initialize array, then you can omit dimensions. For example

    int a[] = {1, 2, 3};
    int b[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
    

Upvotes: 2

BLUEPIXY
BLUEPIXY

Reputation: 40155

int size = 10;
char matrix[size][size];
memcpy(&matrix[0][0],
    "$@*%&#@##@"
    "%$@%#%@*&*"
    "#*&*#@@&%@"
    "%@*$%&#*@&"
    "$*&&&@#%**"
    "##@#&%*$##"
    "&$$#@#@$%*"
    "@$$*&$#*#*"
    "%$*@&@&###"
    "#@%*#&#$%#", size*size);

Upvotes: 1

shafeen
shafeen

Reputation: 2477

Assuming a 10x10 char array:

char **matrix = malloc(sizeof(char*)*10);
int i, len = 10;
for(i = 0; i < len; i++)
    matrix[i] = (char *)malloc(11) // the 11th one is for the null '\0' character (you can ignore it if you like)

Now you can fill the char array locations as you like:

// for example row 1 column 1
matrix[0][0] = '$';

that should help.

After you're done, make sure you use "free" to free the memory allocated.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

This is because C compiler thinks that you are trying to initialize an array of length determined at runtime, even though you supply the size 10 at compile time.

Replacing int size= 10 with #define SIZE 10 will fix this problem:

#define SIZE 10
...
char matrix[SIZE][SIZE] = {
    {'$','@','*','%','&','#','@','#','#','@'},
    {'%','$','@','%','#','%','@','*','&','*'},
    {'#','*','&','*','#','@','@','&','%','@'},
    {'%','@','*','$','%','&','#','*','@','&'},
    {'$','*','&','&','&','@','#','%','*','*'},
    {'#','#','@','#','&','%','*','$','#','#'},
    {'&','$','$','#','@','#','@','$','%','*'},
    {'@','$','$','*','&','$','#','*','#','*'},
    {'%','$','*','@','&','@','&','#','#','#'},
    {'#','@','%','*','#','&','#','$','%','#'}
};

Demo.

Upvotes: 3

Related Questions