1

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**  
1
  • it says in order to upvote i need atleast 15 reputation points.. this is why i was not able to upvote answers.. Commented May 23, 2012 at 6:11

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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]

2 Comments

Great thanks this was the answer i was looking for... I couldn't upvote cos of less reputation points sorry..
@Neha, Most Welcome. You can also accept an answer by clicking on the accept icon which is below the voting icon.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.