#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Can anyone explain my example with pointer and struct?!


    rectangle r is passed as the struct not as
    a pointer. Remember to change the symbol to access the fields of r from
    * -> to . Otherwise your code should be the same. Add a call in main to broken_change_label and demonstrate the problem. My problem is I dont see any difference if r is passed as the struct or as pointer. Whats the problem if I will pass as the struct?? Can anyone explain please???

    Code:
    void broken_change_label(struct rectangle r, char * newlabel) {
    strcpy(r.label,newlabel);
    printf ("broken changed label = %s\n", r.label);
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally Posted by Beginner_in_C
    My problem is I dont see any difference if r is passed as the struct or as pointer. Whats the problem if I will pass as the struct?? Can anyone explain please???
    You don't see the difference? Really? Have you tried to display the contents of the struct after making that function call using a struct instead of a pointer to the struct? Do you see the changes you had made to it in the function?

    Here's a quick test program that illustrates what happens:
    Code:
    #include <stdio.h>
    
    struct Beispiel
    {
        int  num;
        char label[20];
    };
    
    void WithPointer(struct Beispiel *r, int n, const char *s);
    void WithoutPointer(struct Beispiel r, int n, const char *s);
    
    int main()
    {
        struct Beispiel record;
    
        record.num = 1;
        strcpy(record.label, "Label1");
        printf("Initial values:  %d, %s\n", record.num, record.label);
    
        WithoutPointer(record, 2, "Label2");
        printf("After call without pointer:  %d, %s\n", record.num, record.label);
    
        WithPointer(&record, 3, "Label3");
        printf("After call with pointer:  %d, %s\n", record.num, record.label);
    
        return 0;
    }
    
    void WithPointer(struct Beispiel *r, int n, const char *s)
    {
        r->num = n;
        strcpy(r->label, s);
    }
    
    void WithoutPointer(struct Beispiel r, int n, const char *s)
    {
        r.num = n;
        strcpy(r.label, s);
    }
    Here is what the program outputs:
    Initial values: 1, Label1
    After call without pointer: 1, Label1
    After call with pointer: 3, Label3
    Do you see the difference now? Just passing the struct keeps the changes inside the function, while passing a pointer to the struct changes the struct itself.

    Here's why. When you pass an argument to a function, the program evaluates the argument (which in the case of a variable is reading its value) and then copies that value to the function. Always. That means that what the function is using and changing is not the variable you passed to it, but rather a copy of that variable. So none of the changes that the function makes can affect the original variable at all, since it's only a copy that's being changed. And when you return from the function, those copied arguments go away, cease to exist.

    One type of variable you can pass to a function is a pointer. Yes, the function only gets a copy of that pointer to use, but that copy still points to where the original pointer did. Now when the function uses that pointer to change where it's pointing to, it's the original variable being pointed to that is changed. And when you return from that function, those changes still exist.

    So if you pass a struct, the function just gets a copy of that struct and the changes you make to it inside the function go away. But if you pass a pointer to the struct, the original struct itself is changed and those changes continue to exist even after you return from the function.

    Does that make sense?
    Last edited by dwise1_aol; February 8th, 2013 at 11:31 AM.

IMN logo majestic logo threadwatch logo seochat tools logo