Thread: how t properly pass a struct into a funct

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    how t properly pass a struct into a funct

    Before I get into anymore bad habits, please help me with passing a struct into a function. I've never had the need to do this in the past, but it seems like I need it now. If I had a struct like

    Code:
    struct test
    {
        int a, b, c, d , e;
    
        char name1[20];
        char name2[20];
    
        ......
    };
    How exactly I would I write the prototype, definition, and caller? I would want to have several functions have access to the same data set.

    TIA
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    i *think*

    Code:
    struct test
    {
        int a, b, c, d , e;
        char name1[20];
        char name2[20];
    };
    
    // prototype
    
    void structfunc (struct test t);
    
    // pass
    
    struct test q;
    structfunc (q);
    
    // function
    
    void structfunc (struct test t)
    {
      t.a += 4;
      t.name1[6] = 'Q';
    }
    i use typedef struct normally which has a few subtle differences from struct, and i think i remembered them here, but not sure

    edit: obviously this leads out stupid stuff like putting useful data in q before calling structfunc()
    hello, internet!

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hey thanks for the reply moi.... I'll give this a shot.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's almost always better to pass a pointer to the structure instead of passing the structure itself. This way you don't waste memory by copying the structure, all you copy is a pointer.

    -Prelude
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It's almost always better to pass a pointer to the structure
    Best make sure it's a pointer to a const structure, just in case you accidentally try and modify it.

    > i use typedef struct normally which has a few subtle differences from struct
    The only difference is you don't have to keep saying struct. Apart from that, the typedef of a struct is exactly the same as the struct.

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    The pointer sounds like a plan. Thanks to all.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM