global structure variable not accessible in separate file

Hi,

I ran into a problem with accessing global structural variable. just an example:

In file "A.h" I define a structure:
typedef struct
{
int brand;
int model[3];
} MyStruct;

then in file "B.cpp" i define a global variable:
#include "A.h"
#include "B.h"
MyStruct Myproduct = { 5, {1, 2, 3}, };

then in file "B.h" i declare the variable as extern:
extern MyStruct Myproduct;

then in file "C.cpp" i have my function:
#include "B.h"
void myfunct()
{
MyStruct * ptr;
ptr = &Myproduct;
…..something else….
}

Now if i debug and stop right after ptr, then try to see the contents pointed by ptr. all members are 0, not as defined in "B.cpp".

I then tried to define a simple global variable "int try = 10" in B.cpp, and same way to declare, and then created a pointer, then the pointer can get the correct value 10.

I am using Xcode on Mac OS. Can anyone explain why my pointer can not access the global variable? Thanks!
Last edited on
Topic archived. No new replies allowed.