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?
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; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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?
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.