Thread: Passing structures in C functions

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    28

    Passing structures in C functions

    Hi All.

    I'm struggling to get back to grips with C following a 15 year absence...

    I have a structure, as follows:-

    Code:
    typedef struct  {
            long mtype;
            int useragent;
            time_t mdatetime;
            char mtext[4096];
    } busMessage;
    And I want to populate it through a function/procedure by passing it to the function along with some variables as follows:-

    Code:
    void buildMessage( busMessage BM, long MT, int UA ) {
            BM.mtype     = MT;
            BM.useragent = UA;
            BM.mdatetime  = 1005454;
            strcpy(BM.mtext, "A test");
    }
    But when I use this function in the following way:-

    Code:
    int main(void) {
            busMessage myMessage;
    
            buildMessage (myMessage, 4, 4);
            printMessage (myMessage);
            exit(0);
    }
    .... all the fields in myMessage are zero.

    The function printMessage just does this:-

    Code:
    void printMessage( busMessage BM ) {
    
    	printf(ctime(&BM.mdatetime)); // Prints 1st Jan 1970 (correct, as mdatetime must be 0)
    
            printf("Time as int : %d\n", BM.mdatetime); // Zero
    
            printf("UA as int : %d\n", BM.useragent); // Zero
    
            printf("MT as int : %d\n", BM.mtype); // Zero
    
            printf("Payload :  %s\n", BM.mtext); // Blank
    }
    I have a funny feeling this may be a scope issue(?) but can't quite get my head around it.

    Any help would be gratefully received!

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want the function to modify it, you must pass it via a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    28
    Many thanks Elysia.

    Something like (been a long time since I did this):-

    Code:
    busMessage myMessage;
    
    doSomething( &myMessage );
    
    void doSomething( *busMessage BM ) {
     (*BM).field1 = 123;
     // etc....
    }
    ?

    Thanks!

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Close,
    Code:
    void doSomething( busMessage * BM ) {
     (*BM).field1 = 123;
     // etc....
    }
    BTW,
    Code:
    (*BM).field1 = 123;
    is the same as
    Code:
    BM->field1 = 123;
    The latter is easier on the eyes .
    Don't forget if you dereference a NULL pointer bad things happen.

    Perhaps check against NULL
    Code:
    void doSomething( busMessage * BM ) {
        if(BM == NULL)
            return;            /* don't continue */
        BM->field1 = 123;
        // etc....
    }

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    28
    Fantastic - thanks for your help.

    It's all coming back to me slowly...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would rather think an assert is better, since it's clearly a programmer error:
    Code:
    void doSomething( busMessage * BM ) {
        assert(BM);
        BM->field1 = 123;
        // etc....
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Shouldnt the structure be defined like this:

    Code:
    typedef struct busMessage {
            long mtype;
            int useragent;
            time_t mdatetime;
            char mtext[4096];
    };
    Doing it the way you did it would create an instance of the structure...?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, that would generate a compile error - the struct is a typedef, so you must specify a new name for the type (it's not creating an instance).
    Otherwise, loose the "typedef" keyword.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    nvm...............

    EDIT: Sorry im used to C++ where adding typedef in front of a struct has no effect/not the same effect. In C++ doing this will create a new instance:

    Code:
    struct  {
            long mtype;
            int useragent;
            time_t mdatetime;
            char mtext[4096];
    }busMessage;
    
    busMessage.mtype = 1;
    Last edited by 39ster; 05-26-2008 at 12:18 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 39ster View Post
    EDIT: Sorry im used to C++ where adding typedef in front of a struct has no effect/not the same effect.
    No, it does not:

    Code:
    	typedef struct {
    		long mtype;
    		int useragent;
    		time_t mdatetime;
    		char mtext[4096];
    	} a;
    
    	a busMessage;
    	busMessage.mtype = 1;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by Elysia View Post
    No, it does not:

    Code:
    	typedef struct {
    		long mtype;
    		int useragent;
    		time_t mdatetime;
    		char mtext[4096];
    	} a;
    
    	a busMessage;
    	busMessage.mtype = 1;
    ahuh...Well i use structures in C++ like this:

    Defining a structure:
    Code:
    struct SomeStruct
    {
      int a, b, c;
    };
    Create an instance of a struct:
    Code:
    struct
    {
      int a, b, c;
    } blah;
    blah.a = blah.b = blah.c = 32;
    Do both:
    Code:
    struct SomeStruct
    {
      int a, b, c;
    } blah;
    blah.a = blah.b = blah.c = 32;

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, this is more correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  3. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 02:17 PM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM