Passing structs

This is a discussion on Passing structs within the C Programming forums, part of the General Programming Boards category; I am having some problems with passing structs to functions. In one function I take in a struct, which has ...

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Passing structs

    I am having some problems with passing structs to functions.

    In one function I take in a struct, which has a struct within it (call it A). In a function I only alter A (struct A contains arrays).

    fucntion(takes in struct)
    only alter A(struct within the struct as a param)
    Pass the entire struct to another function.

    Now before I pass it, I print some of the array contents. They are as expected.

    Once in the other function, they all become 0.

    The code is lenghty, but if I need to post it I will.

    Maybe I am making a common error, which I dont realize.

    Thanks.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Post the struct definitions, the code that should be doing something and the part that doesn't do what its supposed to. I suggest commenting on what should do what, and what is does do instead.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Passing structs

    Originally posted by MethodMan
    I am having some problems with passing structs to functions.

    Maybe I am making a common error, which I dont realize.

    Thanks.
    Chew on this for a while:
    Code:
    struct mystruct
    {
        char name[BUFSIZ];
    };
    
    
    void fun1( struct mystruct m )
    {
        printf("m.name = %s\n", m.name );
    }
    
    void fun2( struct mystruct m )
    {
        printf("m.name = %s\n", m.name );
        printf("calling fun1( m )\n");
        fun1( m );
        printf("fun2() returns\n");
    }
    
    void fun3( struct mystruct *p )
    {
        if( p )
        {
            memset( p->name, '\0', BUFSIZ );
            strcpy( p->name, "Todd" );
            printf("p->name == %s\n", p->name );
        }
        else
            printf("You screwed up.\n");
        }
    }
    
    int main( void )
    {
        struct mystruct m;
        strcpy( m.name, "BillyBob" );
        fun2( m );
        printf("\nCalling fun3( )\n");
        fun3( &m );
        fun2( m );
        return 0;
    }
    The first two functions just take by value a structure. The third function takes a pointer to a structure, and modifies its contents, so that, when the other two functions are called again, you see the change.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I think I have discovered what the problem could be.

    I am not using pointers to structs that I am passing, instead I am passing the entire struct.

    //Is that how I would declare a function to take in the pointer to a struct?
    myfucntion(struct *mystruct);

    and to call it

    myfucntion(&mystruct);

    Anything else I should know about accessing elements of the structs when I pass them as pointers?

    Thanks

    //Edit, we posted at the same time, Ill take a look

    Thanks.
    Last edited by MethodMan; 04-05-2003 at 10:19 PM.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Okay.

    I have adjusted my function to use pointers to structs.

    Now, I have a nested struct. And within that nested struct, I have arrays.

    I started to print values of the last array, and it starts to print the middle of its contents, and then is followed by junk.

    Am I allowed to go within a struct like that and access only some of the information?

    Thanks.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Followed by junk, is this array supposed to be a string, if so, is it \0 terminated? Otherwise, yes you are allowed to access each member seperatly. With that, you can sort through the array and so forth to find what you need. Dont know if that answers your questions, it would be better to see some code you have so we may help you do what you want to do specifically.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error passing an array of structs
    By Catalyst8487 in forum C++ Programming
    Replies: 10
    Last Post: 12-15-2008, 03:38 PM
  2. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 11:25 AM
  3. Passing structs as parameters
    By 182 in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2006, 10:51 PM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  5. passing structs to functions?
    By Neildadon in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 04:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21