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

Reply

Join Date: Oct 2007
Posts: 4
Reputation: the.alchemist is an unknown quantity at this point 
Solved Threads: 0
the.alchemist the.alchemist is offline Offline
Newbie Poster

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

 
1
  #1
Oct 29th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: the.alchemist is an unknown quantity at this point 
Solved Threads: 0
the.alchemist the.alchemist is offline Offline
Newbie Poster

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

 
0
  #2
Oct 29th, 2007
P.P.S: Using MSVC7.1 compiler
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #3
Oct 30th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,997
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

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

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

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #5
Oct 30th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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