| | |
Problem when passing a structure containing an array of 2D-chars array to a function
![]() |
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Solved Threads: 0
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:
Here's the function that is supposed to take a pointer to an instance of the above structure and modify it :
Now, here is my main :
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
Here's my structure:
C Syntax (Toggle Plain Text)
struct settings { char setting[40][255]; };
Here's the function that is supposed to take a pointer to an instance of the above structure and modify it :
C Syntax (Toggle Plain Text)
void changeSetting(struct settings *tempSettings, char *newValue, int indexValue) { if( indexValue<40){ // to avoid out of boundary problems // according to the debugger, the program crashes in this line : strcpy(tempSettings->setting[indexValue], newValue)}; }
Now, here is my main :
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <main.h> //contains the struct + changeSetting function int main() { struct settings MySettings; changeSetting(&MySettings, "penguin", 0); // supposetly the line above will assign "penguin" to MySettings.setting[0] // but program crashes return 0; // literally *sigh* }
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.
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Solved Threads: 0
Re: Problem when passing a structure containing an array of 2D-chars array to a function
0
#2 Oct 29th, 2007
Re: Problem when passing a structure containing an array of 2D-chars array to a funct
0
#3 Oct 30th, 2007
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:
that semicolon needs to be before the closing }
you can do (*tempSettings).setting[indexValue] or what you just wrote tempSettings->setting[indexValue]. Both are the same.
strcpy(tempSettings->setting[indexValue], newValue)};•
•
•
•
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 ?
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)
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <stdlib.h> struct settings { char setting[40][255]; }; void changeSetting(struct settings *tempSettings, char *newValue, int indexValue) { if (indexValue < 40) { strcpy(tempSettings->setting[indexValue], newValue); } } int main() { struct settings MySettings; changeSetting(&MySettings, "penguin", 0); printf( "Penguin Power=%s\n", MySettings.setting[0] ); return 0; } $ gcc -W -Wall -ansi -pedantic foo.c $ ./a.exe 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.
![]() |
Similar Threads
- delete array and assigns array to pointer (C++)
- Passing 2D Array of Pointers into a function (C++)
- C Program help - Conway's Life - Stumped by some errors. (C)
- Need help with string manipulation and seg faults (C)
- Help with 1.pointers and 2.error checking (C++)
- problem in passing multiple checkbox values (ASP)
- Problem passing structure with array into function (C)
- Sorting arrays of pointers with function? (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: random string
- Next Thread: help with 2d array
| Thread Tools | Search this Thread |
* adobe ansi api array binarysearch centimeter changingto char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax database directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping lowest match meter microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi






