943,735 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1323
  • C RSS
Sep 2nd, 2009
0

assigning data to a 2d char array within structure

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaestroRage is offline Offline
21 posts
since Apr 2008
Sep 2nd, 2009
0

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

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.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Sep 3rd, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MaestroRage is offline Offline
21 posts
since Apr 2008
Sep 3rd, 2009
0

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

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. }
Quote ...
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.

Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Mode of an array in C
Next Thread in C Forum Timeline: Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC