Thread: Passing structure by reference or pointer?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    14

    Question Passing structure by reference or pointer?

    Given the structure below:-
    Code:
    typedef struct {
     int a;
     word b;
    } process;
    By reference:-
    Code:
    main()
    {
     process work;
     /* init structure variable */
     ...
     do_something( &work );
     ...
    }
    By pointer:-
    Code:
    main()
    {
     process work;
     process *ptr_work = &work;
    
     /* init structure variable */
     ...
     do_something( ptr_work );
     ...
    }
    Which would be an optimal performance approach if I want to pass a structure into a function?

    Also, some reading up here and there came across to a site that says "by reference" is a C++ solution (see http://cplus.about.com/od/cprogrammi.../aa010402a.htm ). I have this impression that it is available in C as well, so the other question I have is if it is really a C++ approach, does that mean most, if not all, C compilers would give me error if I use by reference method?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Both ways you've shown are passing by pointer...

    Here's how to declare a function by reference:
    Code:
    void foo(int& a); // notice the amperstand
    Its parameters are passed just like normal variables.

    PS: The first solution seems a lot better to me.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Passing by reference is only allowed in C++. Not under C.
    Passing a pointer is not the same as passing a reference. It's important to understand the difference because it is a source of nasty errors.

    When you pass a pointer, you are still passing by value. However, it's not the object that is being copied, but the pointer that will hold a local copy inside the function.

    Since C doesn't allow passing by reference, all parameters in C hold copies of the arguments passed.

    As for your struct question. Passing by pointer will be a better option on most circumstances. The struct object will not be copied. Just the pointer. For large structs this can be beneficial.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Both solutions will have the same effect, the first one will probably save you a couple of bytes worth of memory; it is also easier to follow.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    No. Both solutions are not the same. Try changing a reference address, for instance.

    Besides this is a C forum. Passing by reference is not recognized by the compiler.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    >Try changing a reference address, for instance.

    Well, the OP didn't try to; so as it stands they are the same. Either you pass the address of a variable, or you pass a pointer which holds the address of the variable - both will do the same thing.

    Of course, if you want to start referencing multiple structures of the same type, a pointer is obviously going to be a better solution.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    14

    Thumbs up

    Ah okie..i confused a pointer method as pass by reference. Thanks for higlighting that out.

    And thanks for the inputs.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    There is a way to simulate a pass by reference in C, and that is to pass a pointer to a pointer. A demonstration:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    struct cat {
            char name[32];
            char bday[12];
    };
    
    void foo(struct cat ** me) {
            printf("%s is located at %p in foo\n", (*me)->name, *me);
    }
    
    int main(void) {
            struct cat *gargamel = malloc(sizeof(struct cat));
            strncpy(gargamel->name, "Gargamel", 32);
            strncpy(gargamel->bday, "4/7/2000", 12);
            printf("%s is located at %p in main\n", gargamel->name, gargamel);
            foo(&gargamel);
            return 0;
    } 
    
    Owner@PAVILION ~
    $ ./a
    Gargamel is located at 003D3DA8 in main
    Gargamel is located at 003D3DA8 in foo
    
    Owner@PAVILION ~
    $
    As you can see, no temporary is made. I like cats.
    Last edited by whiteflags; 07-04-2006 at 10:22 PM.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    1
    thank you whitecats, err... i mean whiteflags....

    the example that you showed is exactly what i was looking for...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM