Question: There is a header file named 'AAA.h'. In this header file we have define a structure called `lrd`, which looks like:

```
struct lrd
{   
int     tc;
char    ptc[5];
char    stc[5];
char    ath[5];
int     vc;
};
struct lrd lr;

```

This header file 'AAA.h' is included in two different files name 'BBB.c' and 'CCC.c'. We have assigned the values for structure variable `lr` in the 'BBB.c' file as following: 

```
lr.tc=tc;
memcpy(lr.ptc,ptc,sizeof(ptc));
memcpy(lr.stc,stc,sizeof(stc));
memcpy(lr.ath,ath,sizeof(ath));
lr.vc=vc;

```

Above source variables take the value from database. And we use structure variable `lr` in the 'CCC.c' file. We are using structure lrd variable as follows:

```
char *ptc()
{
    sprintf(str, "lr.ptc(%s)", lr.ptc);
    trace(str);
    return lr.ptc;
}
char *stc()
{
    sprintf(str, "lr.stc(%s)", lr.stc);
    trace(str);
    return lr.stc;
}

```

**Variable stc gives the wrong value in the 'CCC.c' file.**
Please help me to figure it out.

Answer: 
Your code shows:

```
memcpy(lr.ptc,ptc,sizeof(ptc));
memcpy(lr.stc,stc,sizeof(stc));
memcpy(lr.ath,ath,sizeof(ath));

```

It doesn't show the declarations of `ptc`, `stc` or `ath`. Since you are copying to `lr.ptc` etc, the operand of `sizeof()` should be `lr.ptc` etc, not the size of the source. Or, if you're being really careful, the minimum of the two sizes.

You could/should debug this with:

```
printf("%d (%s)(%s)(%s) %d\n", lr.tc, lr.ptc, lr.stc, lr.ath, lr.vc);

```

immediately after the assignments. If that shows the wrong value, then you know the problem is in the assignments. If that shows the correct values, then you can start looking for stray pointers and other such ghastly problems.

There's no proof that this is the problem because you've not produced an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). Specifically, you've only produced fragments of code, not an integrated set of three source files (though we can infer the header from the information given). And without the code that actually demonstrates the problem (all of it, including definitions for all the variables and functions — but not including any superfluous material), we can only speculate.
External variable should be avoid, but if you still want to use a global variable, you shall do these :

* Declare the variable in a .c file 

struct lrd lr;
* Add the external definition in the .h (AAA.h)

external struct lrd lr;