How do I pass a structure to a function??
-
How do I pass this structure i.e struct s_test { char Name[15]; }test[10]; to a function?? Thanks, grahamoj.
-
How do I pass this structure i.e struct s_test { char Name[15]; }test[10]; to a function?? Thanks, grahamoj.
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
-
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
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!
-
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!
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
-
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
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 = ... }
-
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 = ... }
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
-
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
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;
-
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
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!
-
How do I pass this structure i.e struct s_test { char Name[15]; }test[10]; to a function?? Thanks, grahamoj.