-1

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.

MK Singh
  • 706
  • 1
  • 13
  • 36
  • which compiler you are using?? – Shantanu Banerjee Dec 12 '12 at 07:13
  • Please show your actual code ... describing it to us really doesn't help. – Brian Roach Dec 12 '12 at 07:13
  • Please place code footprint here!! Otherwise its difficult to help – Shantanu Banerjee Dec 12 '12 at 07:17
  • @Brian Roach- I can not give you actual code. structure is very long, it has 122 variables. For assigning the value in y and z we have used memcpy function. Names are dummy but working is same. – MK Singh Dec 12 '12 at 07:20
  • @Shantanu Banerjee gcc compiler – MK Singh Dec 12 '12 at 07:21
  • 2
    Then you need to write a [SSCCE](http://sscce.org/) for here on SO. We really can't guess at what is going wrong. – Brian Roach Dec 12 '12 at 07:21
  • If the definition of `p` is in the header file, your project will not even link but you will get linker errors about `p` being defined multiple times. – Some programmer dude Dec 12 '12 at 07:23
  • 3
    @JoachimPileborg: he could be using a system that grants licence to do it via the 'common' approach, but it is not strictly standard conformant. (It is mentioned in ISO/IEC 9899:2011, Annex J, §J.5.11 **Multiple external definitions** _There may be more than one external definition for the identifier of an object, with or without the explicit use of the keyword extern; if the definitions disagree, or more than one is initialized, the behavior is undefined (6.9.2)._) It's an extension — not standard, but recognized by the standard. – Jonathan Leffler Dec 12 '12 at 07:27
  • @Joachim Pileborg- i have defined p in header and used in both the files. I have not get any linking error. – MK Singh Dec 12 '12 at 07:29
  • 3
    See [What are `extern` variables in C](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/) for an exhaustive explanation of how to do all this. – Jonathan Leffler Dec 12 '12 at 07:29
  • Have you recompiled everything since you last changed anything? One of the first things to check is that everything is up to date. Remove all the object files; rebuild. – Jonathan Leffler Dec 12 '12 at 07:33
  • 1
    Can you show the code that assigns and uses the variables? If you're using string functions with `y`, are you properly null-terminating it? – Barmar Dec 12 '12 at 07:35
  • Dear all, I have edited the question and put SSCCE in the description of the code – MK Singh Dec 12 '12 at 08:41
  • @MKSingh I am missing the "correct (compileable)" aspect here... – glglgl Dec 12 '12 at 08:45
  • Which are the values recovered from database? Which lenght? Is enough space in your struct to store this information? You are using sizeof(var) limits in memcpy operation and you may not keep null terminating value for any char[] field. Check theese values lenght – Tio Pepe Dec 12 '12 at 09:11
  • @Tio pepe- Other variables work fine and give proper value except stc. – MK Singh Dec 12 '12 at 10:04

3 Answers3

1

See what benjarobin says. I am just showing you a small footprint

AAA.h

#ifndef _AAA_H
struct abc
{
 int x;
 char *y;
 char *Z;
};


extern struct abc p;

extern void assign(void);
extern void display (void);
#endif

CCC.c

#include <stdio.h>
#include "AAA.h"

struct abc p; // Deceleration of p
void assign ()
{
    p.x=20;
    p.y="abcd";
    p.Z="xyz0";

    return;
}

BBB.c

#include <stdio.h>
#include "AAA.h"


void display (void)
{
    printf("x = %d, y =%s, z=%s",p.x,p.y,p.Z);

}

DDD.c contains the main function.

#include <stdio.h>
#include "AAA.h"
void main ()
{
    printf("\n\nAssigning Values\n\n");
    assign ();

    printf("\n\nIN BBB.c\n\n");
    display ();

    printf("\n\nIN MAIN\n\n");
    printf("x = %d, y =%s, z=%s",p.x,p.y,p.Z);


}

output is

Assigning Values



IN BBB.c

x = 20, y =abcd, z=xyz0

IN MAIN

x = 20, y =abcd, z=xyz0
Shantanu Banerjee
  • 1,417
  • 6
  • 31
  • 51
  • You changed the contents of the `struct` to use pointers instead of small arrays. That alters things considerably (you can use pointer assignment instead of needing `strcpy()` or `memcpy()` which I was originally going to comment on). You're right; this is roughly what we need from the OP, so +1 for that. – Jonathan Leffler Dec 12 '12 at 15:29
1

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). 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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

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;

benjarobin
  • 4,410
  • 27
  • 21