944,131 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2238
  • C RSS
Oct 29th, 2007
1

Problem when passing a structure containing an array of 2D-chars array to a function

Expand Post »
Hello.

Here's my structure:

  1.  
  2. struct settings {
  3. char setting[40][255];
  4. };

Here's the function that is supposed to take a pointer to an instance of the above structure and modify it :

  1. void changeSetting(struct settings *tempSettings, char *newValue, int indexValue)
  2. {
  3. if( indexValue<40){ // to avoid out of boundary problems
  4. // according to the debugger, the program crashes in this line :
  5. strcpy(tempSettings->setting[indexValue], newValue)};
  6. }


Now, here is my main :


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <main.h> //contains the struct + changeSetting function
  5.  
  6. int main()
  7. {
  8. struct settings MySettings;
  9. changeSetting(&MySettings, "penguin", 0);
  10.  
  11. // supposetly the line above will assign "penguin" to MySettings.setting[0]
  12. // but program crashes
  13.  
  14. return 0; // literally *sigh*
  15. }


I've been debugging for about 13 hours now, not to mention Googling / looking in
mailinglists .


I think it's something to do with the last line in changeSetting function, but I can't point where exactly! Am I supposed to add brackets somewhere ? ( i.e. (*tempSettings)->etc ) or is the problem with me using "->" instead of "." to access the members ? or is it some problem / limitation with C pointers/structures/2D-arrays-in-structures ?

Any help / guidance would be appreciated =)

Thanks in advance,
Axel

P.S: forgot to mention, something identical to this was posted here 2 years ago by another user ( found it when I searched here ). Unfortunately, there was no answer =\
Link : http://www.daniweb.com/forums/thread28036.html
Last edited by the.alchemist; Oct 29th, 2007 at 10:36 pm.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
the.alchemist is offline Offline
4 posts
since Oct 2007
Oct 29th, 2007
0

Re: Problem when passing a structure containing an array of 2D-chars array to a function

P.P.S: Using MSVC7.1 compiler
Reputation Points: 21
Solved Threads: 0
Newbie Poster
the.alchemist is offline Offline
4 posts
since Oct 2007
Oct 30th, 2007
0

Re: Problem when passing a structure containing an array of 2D-chars array to a funct

One of the lovely things about C...

Check line 5 of changeSetting(): you've got the ; on the wrong side of the }.

Every single statement in C must be terminated by a semi-colon. It's the little things that get you...
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 30th, 2007
0

Re: Problem when passing a structure containing an array of 2D-chars array to a funct

There's nothing wrong with your code except:
strcpy(tempSettings->setting[indexValue], newValue)};
that semicolon needs to be before the closing }

Quote ...
I think it's something to do with the last line in changeSetting function, but I can't point where exactly! Am I supposed to add brackets somewhere ? ( i.e. (*tempSettings)->etc ) or is the problem with me using "->" instead of "." to access the members ? or is it some problem / limitation with C pointers/structures/2D-arrays-in-structures ?
you can do (*tempSettings).setting[indexValue] or what you just wrote tempSettings->setting[indexValue]. Both are the same.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 30th, 2007
0

Re: Problem when passing a structure containing an array of 2D-chars array to a funct

Assuming the ; is a typo (it won't even compile), then it seems fine here (cygwin/gcc)
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct settings {
  6. char setting[40][255];
  7. };
  8.  
  9. void changeSetting(struct settings *tempSettings, char *newValue,
  10. int indexValue)
  11. {
  12. if (indexValue < 40) {
  13. strcpy(tempSettings->setting[indexValue], newValue);
  14. }
  15. }
  16.  
  17. int main()
  18. {
  19. struct settings MySettings;
  20. changeSetting(&MySettings, "penguin", 0);
  21. printf( "Penguin Power=%s\n", MySettings.setting[0] );
  22. return 0;
  23. }
  24.  
  25. $ gcc -W -Wall -ansi -pedantic foo.c
  26. $ ./a.exe
  27. Penguin Power=penguin

Your struct is about 10K in size. This shouldn't be a problem if your compiler is a 32-bit compiler, but some old 16-bit compilers used very small (like 3K) stack sizes, and this would obviously blow that right out of the water.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: random string
Next Thread in C Forum Timeline: help with 2d array





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


Follow us on Twitter


© 2011 DaniWeb® LLC