assigning data to a 2d char array within structure

Thread Solved

Join Date: Apr 2008
Posts: 21
Reputation: MaestroRage is an unknown quantity at this point 
Solved Threads: 0
MaestroRage MaestroRage is offline Offline
Newbie Poster

assigning data to a 2d char array within structure

 
0
  #1
Sep 2nd, 2009
sorry for the long title, but it pretty much sums it all up. I have a 2 test structures, and in one I have a 2d char array, which is essentially a 1d string list.

However I can't seem to interact with it on the same level as I can a normal 2d char array. Using

char test[][]={"stuff", "stuff2"}; approach won't work, the curly brackets makes it unhappy, removing the curly brackets helps, however from debug tests it seems that nothing gets written.

Can anybody help me assign some data to a 2d char array within this structure?
  1. #include <stdio.h>
  2.  
  3. typedef struct a{
  4. int a;
  5. }A;
  6.  
  7. typedef struct b{
  8. char ba[25][12];
  9. int bb[12];
  10. }B;
  11.  
  12. B test;
  13.  
  14.  
  15. int main (){
  16. test.ba[25][12]="a","b";
  17. printf ("%s\n%s", test.ba[0][0], test.ba[0][1]);
  18. //returns null here
  19. return 0;
  20. }

I get an access violation error if I try to use strcpy so i'm either going outside the array bounds or am trying to write to read only memory.

Huge thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: assigning data to a 2d char array within structure

 
0
  #2
Sep 2nd, 2009
You can use {} with arrays only when initializing.
  1. char ba[25][12] = {"item[0][0]", "item[0][1]", "item[0][2]",......, "item[0][11]",
  2. "item[1][0]", "item[1][1]", "item[1][2]",......, "item[1][11]",
  3. .
  4. .
  5. .
  6. "item[24][0]", "item[24][1]", "item[24][2]",......, "item[24][11]",
  7. }

After initializing you need to assign it using [][] = value
  1. for(int i = 0; i < 25; i++) {
  2. for(int j = 0; j < 12; j++) {
  3. ba[i][j] = "item[" + i + "][" + j + "]";// This string concatination might not work with your compiler
  4. }
  5. }


So your assigning ba[25][12] = "ab", "ba";

here ba[25][12] is out of array range

should be ba[24][11] = "somthing";

and you are printing ba[0][0] and ba[0][1] which are not been assigned.

Hope You get it.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 21
Reputation: MaestroRage is an unknown quantity at this point 
Solved Threads: 0
MaestroRage MaestroRage is offline Offline
Newbie Poster

Re: assigning data to a 2d char array within structure

 
0
  #3
Sep 3rd, 2009
thank you luckychap for your answer, however I am a bit confused about something.

One can say char a[] is simply a string since a string in C is only an array of chars.

So when one says char a[][] is that not a 1d array of strings?

From your instantiation it seems like to me it treats it like a 2d array of strings.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: assigning data to a 2d char array within structure

 
0
  #4
Sep 3rd, 2009
If you defer initialization of the struct object, you can do it all in one go:
  1. #include <stdio.h>
  2.  
  3. typedef struct a
  4. {
  5. int a;
  6. } A;
  7.  
  8. typedef struct b
  9. {
  10. char ba[2][12];
  11. int bb[12];
  12. } B;
  13.  
  14. int main()
  15. {
  16. B test =
  17. {
  18. {"a", "b"},
  19. {1, 2, 3, 4, 5}
  20. };
  21. int x;
  22.  
  23. for (x = 0; x < 2; ++x) printf("%-2.12s", test.ba[x]);
  24. putchar('\n');
  25. for (x = 0; x < 12; ++x) printf("%-2d", test.bb[x]);
  26. putchar('\n');
  27.  
  28. return 0;
  29. }
One can say char a[] is simply a string since a string in C is only an array of chars.
That is only half the story. A string in C requires two parts: an array of character type and a terminating '\0' character. If you have an array of char but not '\0' at the end, it is not a string. A string also does not have to be the char type. C supports two character types for strings where there are string constants and string functions defined. char is the single byte character type and wchar_t is the multibyte character type.

So when one says char a[][] is that not a 1d array of strings?
It is a 2D array of char. If the inner arrays are all strings then it is also a 1D array of strings. You can use it either way as long as the requirements for a string are met.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC