简体   繁体   中英

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.

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.

  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 } }; 
int size = 10;
char matrix[size][size];
memcpy(&matrix[0][0],
    "$@*%&#@##@"
    "%$@%#%@*&*"
    "#*&*#@@&%@"
    "%@*$%&#*@&"
    "$*&&&@#%**"
    "##@#&%*$##"
    "&$$#@#@$%*"
    "@$$*&$#*#*"
    "%$*@&@&###"
    "#@%*#&#$%#", size*size);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM