passing struct to function by reference

This is a discussion on passing struct to function by reference within the C++ Programming forums, part of the General Programming Boards category; Hey again another question. How can I pass a struct to a function by reference. Code: int myfunction(*) { // ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    19

    passing struct to function by reference

    Hey again another question. How can I pass a struct to a function by reference.

    Code:
    int myfunction(*)
    {
      
      // will do stuff to struct
     return 0;
    };
    
    int main()
    {
    
     struct data 
     {
        int a;
        int b;
        int c;
      };
    
    data d;
    
    myfunction(*);]
    
    return 0;
    }
    This is just example code of course. What is it that I need to put in the place of the *'s? Reading from a book the way to pass a variable by reference is (myVar &hisVar). Is this correct? If not what is. What bit do I need to put where. I am very confused. I may be jumping ahead of myself but I find if I don't try adn get the complicated bits done I get bored and give up. I can wite hello world in lots of languages but can't do anything useful in any of them.

    Thanks

    Ironfistchamp

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
     struct data 
     {
        int a;
        int b;
        int c;
      };
    
    // C-ctyle
    int myfunction(struct data* d)
    {
      
      // will do stuff to struct
      d->a = 1;
      d->b = 2;
      d->c = 3;
     return 0;
    };
    
    
    // C++ style
    int myfunction(data& d)
    {
      
      // will do stuff to struct
      d.a = 1;
      d.b = 2;
      d.c = 3;
     return 0;
    };
    
    
    int main()
    {
    
    data d;
    
    // C-style
    myfunction(&d);
    
    // C++ style
    myfunction(d);
    return 0;
    }

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    First and foremost, do yourself a favor and move the struct outside main().

    You pass a struct the same way you pass any other object...

    Code:
    struct struct data  {
        int a;
        int b;
        int c;
    };
    
    int sumX (data &obj) {
        return obj.a + obj.b + obj.c;
    }
    
    int main() {
    
        data myData;
    
        myData.a = 0;
        myData.b = 1;
        myData.c = 2;
    
        int total = sumX(myData); // total will store the value 3
    
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    19
    Exceelent that's done the trick thanks. Does it matter having the struct outside main? I was told not to as it is like a variable and you should refrain from making global variables. True or not?

    Thanks again

    Ironfistchamp

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Move the struct inside main and run the example I gave you... you will see why you should declare them outside main()
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    19
    Aha got ya. Ok new problem. I wanted to use my own header (want to get the hang of it and make my code more "modular"). So I put it in myfunc.h, put the #include "myfunc.h" at the top and went from there. But I get the error that the struct is not defined in the scope even though it is global. Why?

    Thanks

    Ironfistchamp

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Not sure what you are doing wrong. You put the include in which file? and what did you put inside the header file?

    Anyways, the tutorials explain about header files and how to use them. Check there.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    19
    All I have is the main.cc and myfunc.h files. All the includes are in the main.cc file and the struct is defined right after that. The myfunc.h file only has the function we worked out before. Should be right shouldn't it. You must be able to pass stuff to functions in header files.

    I don't know perhaps I have the wrong end of the stick. The book I have (Accelerated C++) is good but you have to really think about what is being said. It has great info but isn't written in a terribly friendly way.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Ah. I see... it's the function you moved inside the header file...

    Well, without entering into great details (again I urge you to read about header files tutorials), what probably is happening is that you are making the includes first and only then defining the struct...

    But a struct definition also declares a new name (the name of the struct). And your function can't see that name because it's being included before the struct is defined. You have to follow the same structure as before.

    The include for the function must be done after the struct is defined and before main().

    However, let me tell you at this point that what you are doing is not a best practice. But since you are just trying to see how it works...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    19
    Wow thanks. OK I shall read more about headers. I looked for a tutorial here but couldn't see one dedicated to just headers. Is it included under a different name.


    I really want to thank you for your replies. This has got to be one of the best forums. Generally I spend days waiting for some elitest to tell me I am a "n00b" (well not in the Ubuntu forums they rock too )

    Thanks again

    Ironfistchamp

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by ironfistchamp
    Exceelent that's done the trick thanks. Does it matter having the struct outside main? I was told not to as it is like a variable and you should refrain from making global variables. True or not?

    Thanks again

    Ironfistchamp
    Not true. Whoever told you that was wrong. structures do nothing more than declare a user-defined data type, it does not create an object of that type. And all data types must be defined before they can be used, so its impossible to use a structure before it is defined. And that is another reason for putting structure definitions in header files. For examples of how it is done just look in the header files that were shipped and installed with your compiler. There is nothing sacred about them and they will not make you go blind if you look directly at them. Pick an easy one, like string.h to see what all it contains.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    19
    Ah good idea will do. If I was to put the struct in a header file that would be a good use for my own headers right?

    Thanks again

    Ironfistchamp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 11:25 AM
  4. passing struct member to function
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 01-06-2003, 04:55 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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