Hi, I was just wondering whether it is possible to pass single elements of a structure to a function as arguments. For example, structure consists of a few int, but I am only interested in passing, let's say, two of them. Is it possible at all?
This is a discussion on structure question: passing to functions within the C Programming forums, part of the General Programming Boards category; Hi, I was just wondering whether it is possible to pass single elements of a structure to a function as ...
Hi, I was just wondering whether it is possible to pass single elements of a structure to a function as arguments. For example, structure consists of a few int, but I am only interested in passing, let's say, two of them. Is it possible at all?
Yes.
Code:struct foo { int iVal1; int iVal2; }; void func(int i1, int i2) { // Do something with i1 and i2 } int main(void) { struct foo bar; bar.iVal1 = 5; bar.iVal2 = 10; func(bar.iVal1,bar.iVal2); return 0; }
I used to be an adventurer like you... then I took an arrow to the knee.
Oh, so there's nothing special to write in function declaration/definition as type, just int or float or whatever I use from the structure... I just need to modify the function call to use the structure elements. All right, thank you very much!
But it's useless. Why not just pass the entire address of the structure?
Arrogance breeds bad code
Why? So you can duplicate all of your functions that work with basic types so that they take arguments of every structure type you make? Talk about reinventing the wheel.
Quzah.
Hope is the first step on the road to disappointment.