Handout 33
Fun With Structs
The question of interest is, "when we have an array as part of a struct, and that struct is passed by value
to a function, is a copy of the array stored on the run-time stack?" In the following example the addresses
of the struct and another variable are displayed. Clearly the copy of the array is stored on the run-time stack.
#include <iostream.h>
struct myStruct {
int x;
float y[100];
};
void tester(int, struct myStruct);
int main() {
struct myStruct theStruct;
cout << "in main address = " << &theStruct << endl;
cout << "in main struct int address = " << &theStruct.x << endl;
cout << "in main struct array address = " << &theStruct.y << endl;
tester(1, theStruct);
return 0;
}
void tester(int i, struct myStruct a) {
cout << "in tester struct address = " << &a << endl;
cout << "in tester int i address = " << &i << endl;
cout << "in tester struct int address = " << &a.x << endl;
cout << "in tester struct array address = " << &a.y << endl;
}
/* program output
in main address = 0x0012FDEC
in main struct int address = 0x0012FDEC
in main struct array address = 0x0012FDF0
in tester struct address = 0x0012FC0C
in tester int i address = 0x0012FC08
in tester struct int address = 0x0012FC0C
in tester struct array address = 0x0012FC10
*/