Thread: after the fuction, same struct variacles.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    after the fuction, same struct variacles.

    Another question:

    the reason that the variables does not change is because:
    the whole box (made for the function) is deleted when going back in main. What i mean is that the variables CHANGE but they do not return to the main function so they can printed. Am i correct?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Foo{
    	int x;
    	int y;
    };
    
    void f1(struct Foo foo){
    	foo.x=12;
    	foo.y=13;
    }
    
    int main(void){
    	struct Foo foo;
    
    
    	foo.x=10;
    	foo.y=11;
    	
    	printf("x: %d\n", foo.x);
    	printf("y: %d\n", foo.y);
    
    		f1(foo);
    
    	printf("x: %d\n", foo.x);
    	printf("y: %d\n", foo.y);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Yes. You're simply passing a copy of the struct to f1(), so any changes are made to a copy. It's like if you make a photocopy of, say, a contract, and give that to somebody. If he burns his copy, you still have yours.

    Contrast this to passing a pointer to a struct: if you do that, you can modify the struct and it will be reflected in the caller. This is like telling somebody where you keep the contract; now if it is burned, it's yours that gets destroyed.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    81
    i am not sure if this is what you are talking about, but there is a problem (red markers)...what is going wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Foo{
    	int x;
    	int y;
    }group1,group2;
    
    void f1(struct Foo *foo){
    	foo.x=12;
    	foo.y=13;
    }
    
    int main(void){
    	struct Foo foo;
    
    	foo.x=10;
    	foo.y=11;
    	
    	printf("x: %d\n", foo.x);
    	printf("y: %d\n", foo.y);
    
    		f1(&foo);
    
    	printf("x: %d\n", foo.x);
    	printf("y: %d\n", foo.y);
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's a bit confusing because you named the pointer the same as the struct instance-- please don't do that! If nothing else, put a p in the front of the pointer's name, like *pfoo.

    If you are using a pointer to a struct, then use the pointer to struct access operator ->, not the struct member access operator (the dot).

    Code:
    void f1(struct Foo *foo){
    	foo->x=12;
    	foo->y=13;
    }
    Last edited by Adak; 06-20-2010 at 06:47 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    81
    ok i' ll try to put "p".
    insteed of the code you wrote can we use:
    Code:
    	*(foo.x)=12;
    	*(foo.y)=13;
    ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, because then you are dereferencing foo.x, not dereferencing foo to access x.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by laserlight View Post
    No, because then you are dereferencing foo.x, not dereferencing foo to access x.
    apart from the the "->" i have seen somewhere something like this that i wrote....is there another way to do it?

  8. #8
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    I think you are thinking of
    Code:
    (*foo).x=12
    see the dot . is evaluated first before *. So to force the dereference to occur first you add the parenthesis. This is exactly the same as saying

    Code:
    foo->x=12
    Last edited by jimtuv; 06-20-2010 at 07:05 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    81
    exactly!! thanks....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM