Neha
Neha

Reputation: 49

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**  

Upvotes: 1

Views: 2109

Answers (3)

Jay
Jay

Reputation: 24905

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]

Upvotes: 2

Vicky
Vicky

Reputation: 1235

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.

Upvotes: 3

Prasad G
Prasad G

Reputation: 6718

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.

Upvotes: 0

Related Questions