In file "A.h" I define a structure:
typedefstruct
{
int brand,
int model[3],
} MyStruct;
in file "B.cpp" i define a global variable of MyStruct type:
#include "A.h"
#include "B.h"
MyStruct Myproduct =
{ 5, // brand
{1, 2, 3} // model
};
in file "B.h" i declare the variable as extern:
extern MyStruct Myproduct;
in file "C.cpp" i have my function:
#include "B.h"
void myfunct()
{
MyStruct * ptr;
ptr = &Myproduct;
...something else...
}
Then in my main function, i call the myfunct(). And when i debug and set breakpoint right after ptr, then try to see the contents pointed by ptr, all members are 0, not as defined in "B.cpp".
But if i move the structure definition from A.h to B.cpp, then the pointer ptr can correctly show the value of my global variable.
I am using Xcode on Mac OS. Can anyone explain why it did not work when the definition of structure type and global variable are in separate files? Thanks!