Thread: pointer to structure question

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    pointer to structure question

    What is the advantage of usinga pointer to a structure as a parameter to a funciton, instead of the structure itself?
    I think it makes it more efficient because the structure is not copied. Am I right?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you want to change the members of a struct in a function, you have to pass the struct's address.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    Can you elaborate MacGyver?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <stdio.h>
    
    struct bleh
    {
    	int x;
    };
    
    void breakstruct(struct bleh);
    
    int main(void)
    {
    	struct bleh somestruct;
    	
    	somestruct.x = 5;
    	breakstruct(somestruct);
    	printf("somestruct.x = %d\n",somestruct.x);
    	
    	return 0;
    }
    
    void breakstruct(struct bleh strct)
    {
    	strct.x = 100;
    }
    Output:

    Code:
    somestruct.x = 5
    The function can't change the value of x of the struct passed to it. If a pointer to that struct was passed, then it could alter it.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    27
    So passing a structure as a parameter is not allowed?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    No, it's allowed, but it's like passing an int to a function. If you want to change the integer that you pass inside the function and make it reflect the changes inside the function that called it (like main()), you can't do that. You have to pass the address of the int.

    If you know about the difference between passing an int by value and passing it by reference, then it's the same thing with structs. Don't overcomplicate it just because it's a struct.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    27
    Thanks MacGyver!!

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    By having a function take a pointer to a structure, such as to load a BMP file and fill in the info header data while reading from a file. It makes it more convenient and easier to do. Instead of the dot to access an element, you'd have the "->" instead if a pointer is used. My LoadFile function takes such a pointer.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    And if you ever learn C++ you will also learn passing by reference using '&' which is handy when passing very large objects to a function as much less overhead is made. But this is a C board so ignore that
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. 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
  5. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM