C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2013, 09:17 PM
Beginner_in_C Beginner_in_C is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 Beginner_in_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 13 sec
Reputation 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);
}

Reply With Quote
  #2  
Old February 7th, 2013, 11:39 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,137 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 34 m 49 sec
Reputation Power: 1974
Quote:
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:
Quote:
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 10:31 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Can anyone explain my example with pointer and struct?!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap