Reputation: 341
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
Reputation: 2106
So the solution is to give computer information about size of array before compilation. So you can define size with macros
#define size 10
Or to create array dynamically on heap using malloc (and other memory allocating functions)
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
Reputation: 40155
int size = 10;
char matrix[size][size];
memcpy(&matrix[0][0],
"$@*%&#@##@"
"%$@%#%@*&*"
"#*&*#@@&%@"
"%@*$%&#*@&"
"$*&&&@#%**"
"##@#&%*$##"
"&$$#@#@$%*"
"@$$*&$#*#*"
"%$*@&@&###"
"#@%*#&#$%#", size*size);
Upvotes: 1
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
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] = {
{'$','@','*','%','&','#','@','#','#','@'},
{'%','$','@','%','#','%','@','*','&','*'},
{'#','*','&','*','#','@','@','&','%','@'},
{'%','@','*','$','%','&','#','*','@','&'},
{'$','*','&','&','&','@','#','%','*','*'},
{'#','#','@','#','&','%','*','$','#','#'},
{'&','$','$','#','@','#','@','$','%','*'},
{'@','$','$','*','&','$','#','*','#','*'},
{'%','$','*','@','&','@','&','#','#','#'},
{'#','@','%','*','#','&','#','$','%','#'}
};
Upvotes: 3