DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Problem when passing a structure containing an array of 2D-chars array to a function (http://www.daniweb.com/forums/thread94744.html)

the.alchemist Oct 29th, 2007 10:35 pm
Problem when passing a structure containing an array of 2D-chars array to a function
 
Hello.

Here's my structure:


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 :

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 :


#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

the.alchemist Oct 29th, 2007 10:37 pm
Re: Problem when passing a structure containing an array of 2D-chars array to a function
 
P.P.S: Using MSVC7.1 compiler

Duoas Oct 30th, 2007 12:31 am
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...

Aia Oct 30th, 2007 12:58 am
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.

Salem Oct 30th, 2007 5:07 am
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)
#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.


All times are GMT -4. The time now is 10:23 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC