Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I pass a structure to a function??

How do I pass a structure to a function??

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IrishSonic
    wrote on last edited by
    #1

    How do I pass this structure i.e struct s_test { char Name[15]; }test[10]; to a function?? Thanks, grahamoj.

    J E 2 Replies Last reply
    0
    • I IrishSonic

      How do I pass this structure i.e struct s_test { char Name[15]; }test[10]; to a function?? Thanks, grahamoj.

      J Offline
      J Offline
      Jesse Evans
      wrote on last edited by
      #2

      grahamoj, a structure is just a fancy variable, so you can pass it just as you would an int or a float or double. If it's really big, you'll probably want to pass it as a reference: void foo (struct s_test * t); // the prototype foo (&test); // the call If you pass it as a value, you access the structure elements using the . operator; if you pass it as a reference use the -> operator. Hope this helps! 'til next we type... HAVE FUN!! -- Jesse

      J 1 Reply Last reply
      0
      • J Jesse Evans

        grahamoj, a structure is just a fancy variable, so you can pass it just as you would an int or a float or double. If it's really big, you'll probably want to pass it as a reference: void foo (struct s_test * t); // the prototype foo (&test); // the call If you pass it as a value, you access the structure elements using the . operator; if you pass it as a reference use the -> operator. Hope this helps! 'til next we type... HAVE FUN!! -- Jesse

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        Technicaly that is not a reference, that is a pointer. There is no such thing as passing by reference in C. You could call it a reference if you want to but no one else would know you where actualy talking about a pointer unless you specificaly mention the C language, and then they might figure it out. // C Memory pointer void foo(struct s_test* t) { int nSize = t->m_Size; } // C++ Reference void foo(s_test& t) // or void foo(struct s_test& t) { int nSize = t.GetSize(); } // C++ Memory pointer void foo(s_test* t) // or void foo(struct s_test* t) { int nSize = t->m_Size; } Trust in the code Luke. Yea right!

        J 1 Reply Last reply
        0
        • J John R Shaw

          Technicaly that is not a reference, that is a pointer. There is no such thing as passing by reference in C. You could call it a reference if you want to but no one else would know you where actualy talking about a pointer unless you specificaly mention the C language, and then they might figure it out. // C Memory pointer void foo(struct s_test* t) { int nSize = t->m_Size; } // C++ Reference void foo(s_test& t) // or void foo(struct s_test& t) { int nSize = t.GetSize(); } // C++ Memory pointer void foo(s_test* t) // or void foo(struct s_test* t) { int nSize = t->m_Size; } Trust in the code Luke. Yea right!

          J Offline
          J Offline
          Jesse Evans
          wrote on last edited by
          #4

          Not to pick nits with you, but in the C 'bible' ("The C Programming Language", Kernighan & Ritchie) chapter 1.8 ("Arguments - Call by value") passing arguments by value is differentiated from other methods, which are referred to as "call by reference". Argument passing by value vs. by 'reference' is also mentioned several other places in the book. So, although this technique is implemented by passing pointers, it can be referred to in more than one way. Still, your 'point' is well taken and your examples illustrate the issue quite nicely, much better than mine did. 'til next we type... HAVE FUN!! -- Jesse

          R J 2 Replies Last reply
          0
          • J Jesse Evans

            Not to pick nits with you, but in the C 'bible' ("The C Programming Language", Kernighan & Ritchie) chapter 1.8 ("Arguments - Call by value") passing arguments by value is differentiated from other methods, which are referred to as "call by reference". Argument passing by value vs. by 'reference' is also mentioned several other places in the book. So, although this technique is implemented by passing pointers, it can be referred to in more than one way. Still, your 'point' is well taken and your examples illustrate the issue quite nicely, much better than mine did. 'til next we type... HAVE FUN!! -- Jesse

            R Offline
            R Offline
            Rob McBean
            wrote on last edited by
            #5

            I see the confusion here...Passing by reference in C means to pass a pointer which means that the called function can modify it. Passing by value means you are passing a copy of the variable. C++ introduced the concept of the reference type. A reference is like a pointer, but unlike a pointer is must be bound to a specific variable. K&C have a very good explanation of pass by value/reference and pointers, however that was written before c++ came along. See Stroustrups book on C++ for an explanation of C++ references. Reference types do not access their members with -> but with . pass structure by reference in c: main { somestruct x; function(&x) } function( somestruct* y) { y->member = ... } same thing in C++ main { somestruct x; somrstruct& y = x; function(y) } function(somestruct& ref) { y.member = ... }

            J 1 Reply Last reply
            0
            • R Rob McBean

              I see the confusion here...Passing by reference in C means to pass a pointer which means that the called function can modify it. Passing by value means you are passing a copy of the variable. C++ introduced the concept of the reference type. A reference is like a pointer, but unlike a pointer is must be bound to a specific variable. K&C have a very good explanation of pass by value/reference and pointers, however that was written before c++ came along. See Stroustrups book on C++ for an explanation of C++ references. Reference types do not access their members with -> but with . pass structure by reference in c: main { somestruct x; function(&x) } function( somestruct* y) { y->member = ... } same thing in C++ main { somestruct x; somrstruct& y = x; function(y) } function(somestruct& ref) { y.member = ... }

              J Offline
              J Offline
              Jesse Evans
              wrote on last edited by
              #6

              Well, I cut my eye teeth on K&R... looks like I should curl up with Stroustrup's book soon. 'til next we type... HAVE FUN!! -- Jesse

              G 1 Reply Last reply
              0
              • J Jesse Evans

                Well, I cut my eye teeth on K&R... looks like I should curl up with Stroustrup's book soon. 'til next we type... HAVE FUN!! -- Jesse

                G Offline
                G Offline
                Gary R Wheeler
                wrote on last edited by
                #7

                By all means get Stroustrup's book. I just bought the third edition. The 3rd updates it according to the current standard, and adds a description of the standard libraries.


                Software Zen: delete this;

                1 Reply Last reply
                0
                • J Jesse Evans

                  Not to pick nits with you, but in the C 'bible' ("The C Programming Language", Kernighan & Ritchie) chapter 1.8 ("Arguments - Call by value") passing arguments by value is differentiated from other methods, which are referred to as "call by reference". Argument passing by value vs. by 'reference' is also mentioned several other places in the book. So, although this technique is implemented by passing pointers, it can be referred to in more than one way. Still, your 'point' is well taken and your examples illustrate the issue quite nicely, much better than mine did. 'til next we type... HAVE FUN!! -- Jesse

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #8

                  I should have worded my replay earlier differently. I have not needed to read a book on C in years and I have know idea where my copy of "The C Programming Language" is. But here is the way I look at it (and yes I have been influenced by C++). *pData is a dereferenced pointer (a dereference reference pointer to a memory address) and is therefore a reference to the data its self. In outher words pData could be considered an indirect reference to the data (although it looks like a direct reference to me). Now then in C++ pData is a pointer to the data and *pData is a reference to the data. I beleive the first C++ compilers converted C++ code to C code and then use the already available C compiler (at least on UNIX). Wether you agee with the above analiysis is only relevent to the acadimics of the subject though. Do mostly to my influence by C++, I perfer to differentiate between the two, since I do not wish to confuse outhers as to what I am actualy talking about. Other than my comment, I did make a major mistake in the examples I gave the newbie. I forgot to give examples of how to call the function which sort of defeated the whole purpose. foo(&myData); // C/C++ pass by address // C++ pass by value or pass by reference depending // on wether the function takes a value or refer a // reference for(myData); Trust in the code Luke. Yea right!

                  1 Reply Last reply
                  0
                  • I IrishSonic

                    How do I pass this structure i.e struct s_test { char Name[15]; }test[10]; to a function?? Thanks, grahamoj.

                    E Offline
                    E Offline
                    Ernesto D
                    wrote on last edited by
                    #9

                    Have you tried using a pointer? void MyFunction(s_test* pTest) { .............. }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups