problem accessing global variable

Hi,

I have a problem with global variable of type which i defined by myself. here is what happened:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
In file "A.h" I define a structure:
typedef struct
{
    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!
Topic archived. No new replies allowed.