after the fuction, same struct variacles.

This is a discussion on after the fuction, same struct variacles. within the C Programming forums, part of the General Programming Boards category; Another question: the reason that the variables does not change is because: the whole box (made for the function) is ...

  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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    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
    21,588
    No, because then you are dereferencing foo.x, not dereferencing foo to access x.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21