简体   繁体   中英

How to initialize three dimensional char array without using pointers in c

How to initialize three dimensional char array without pointers in c and access it?
I tried the following:

char card[1][3][15]={            
                      {"iron","man"},  
                      {"contagious","heide"},  
                      {"string","middle"}  
                     };  

but I am getting

  **Error:too many initializers**  
  **Warning: Array is only partially initialized**  

Lets take a simple example...You can use your own values instead of these integers:

declaration:

int arr[2][3][4] = { { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} },
                     { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} } };

I hope, it is clear to you.

Considering your example itself:

I think it should be

char card[1][3][15]={ {"iron","man", "contagious"}};

What this means is that you can effectively create 3 char arrays each of length 15. Your first dimension of 1 doesn't have much effect.

So, you can make it like

char card[2][3][15]={ {"iron","man", "contagious"},
                      {"iron","man", "contagious"}};

So, for your simple understand, the number of rows indicate the first dimension, the number of columns in each row indicates the second dimension and the number of elements(in this case chars) in each column indicates the 3rd dimension.

So, now you can see that for the data in your question, you should declare the array as char char[3][2][15]

char card[1][3][15]={ { {"iron","man"},{"contagious","heide"},{"string","middle"}}
                     };

You should put another braces brackets inside. I think it will be helpful to you.

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