How can you make a 2 dimensional array in C?

My brain passed very vaguely recently, and I can’t remember for my life why the following C code:

char a[3][3] = { "123", "456", "789" }; char **b = a; 

Raises the following warning:

 warning: initialization from incompatible pointer type 

Can anyone explain this to me.

Thanks.

+8
c arrays pointers initialization
source share
5 answers
 char (*b)[3] = a; 

Declares b as a pointer to char arrays of size 3. Note that this is not the same as char *b[3] , which declares b as an array of 3 char pointers.

Also note that char *b = a is incorrect and still issues the same warning as char **b = a .

+10
source share

Try it,

  char a[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9' }}; char *b = &a[0][0]; 

Since a is an array of characters of arrays, you need to initialize them as a character.

+1
source share

It is right. a is a pointer.

char *b defines a pointer to char.

char **b defines a pointer to a pointer to char.

0
source share

the problem is that ** is not statically allocated.

you can run this simple version with the following:

 char a[3][3] = {"123", "456", "789"}; char *b[3] = {a[0], a[1], a[2]}; 
0
source share

a is still a pointer to char:

 char* b = a; 
-one
source share

Source: https://habr.com/ru/post/651153/


All Articles