Thread: Passing Typedef Struct to a function

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Passing Typedef Struct to a function

    Hello,

    I am a bit confused as to why this code compiles, but is not working, and how to fix it. Can you help? Thx

    Code:
    #include <stdlib.h>
    
    typedef struct {
    	int x;
    } Stype;
    
    void Fun(Stype a) {
    	a.x = a.x + 1;
    }
    
    int main() {
    	Stype sexample;
    	sexample.x = 0;
    	
    	Stype arr[10];
    	arr[0] = sexample;
    	
    	Fun(arr[0]);
    	
    	printf("%d", arr[0].x); // why not 1?
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    When you pass arr[0] to Fun, you pass it by copy. So, the change you make to the copy is not reflected back in main. If you want it the change to be reflected back in main, pass your value by reference (or maybe pointer).

    If you want to see 1 as the output, you should do something like this:
    Code:
    void Fun(Stype& a) {
          a.x++;
    }
    
    int main() {
          .....
          Fun(arr[0]);
          .....
    }
    
    // or maybe,
    
    void Fun(Stype* a) {
          *a.x++;
    }
    
    int main() {
          .....
          Fun(&arr[0]);
          .....
    }
    Last edited by Zeus_; 03-27-2020 at 04:03 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    But I thought I was passing an element of the array, not the whole array?

    Are you sure your examples compile? This should work

    Code:
    #include <stdlib.h>
    
    typedef struct {
    	int x;
    } Stype;
    
    void Fun(Stype *a) {
    	a->x = a->x + 1;
    }
    
    int main() {
    	Stype sexample;
    	sexample.x = 0;
    	
    	Stype arr[10];
    	arr[0] = sexample;
    	
    	Fun(&arr[0]);
    	
    	printf("%d", arr[0].x); // why not 1?
    	
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by serge View Post
    Are you sure your examples compile? This should work
    No, it doesn't compile. Zeus is confused between C++ and C.
    Your code is correct, although using ++ is more usual.

    But I thought I was passing an element of the array, not the whole array
    Passing a pointer to the first element is essentially passing the whole array since in your function you could increment a to access the next element.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    My bad. I should have written (*a).x++
    ++ has higher precedence than dereferencing a pointer. I was busy with something else at the time you posted and I happened to write wrong code. Sorry Nonetheless, the reference version works right.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    115
    Thx a lot. I am doing a pure c project atm, and it happens to me all the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct to function
    By silentkarma in forum Windows Programming
    Replies: 15
    Last Post: 03-07-2008, 12:57 AM
  2. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  3. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM
  4. How to pass a typedef struct to function?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 09-19-2004, 04:06 PM
  5. passing struct to function
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 12:52 AM

Tags for this Thread