variable definiton / variable declaration

Discussion in 'C Programming' started by baumann@pan, May 11, 2005.

  1. baumann@pan

    baumann@pan Guest

    Hi all,

    in a .h header file
    i want to declare a var as

    extern struct mytype a[200];


    after I include the header file in a dot c file


    and define the var in the global scope of the dot c file as

    struct mytype a[200];

    but the compiler complains the redefinition occurs,

    how to cope with it? thanks in advance.


    baumann at pan
     
    baumann@pan, May 11, 2005
    #1
    1. Advertising

  2. baumann@pan

    baumann@pan Guest

    btw, the var may be used in other dot c file,

    in the h header file, i also tried declare the var as
    extern struct mytype *a;

    it differs nothing.

    I know if in .h file

    i declare
    extern int a;

    in dot c file,
    i define
    int a = 1;

    it would ok, why??

    what's difference between the two variables?
     
    baumann@pan, May 11, 2005
    #2
    1. Advertising

  3. baumann@pan

    -berlin.de Guest

    baumann@pan <> wrote:
    > in a .h header file
    > i want to declare a var as


    > extern struct mytype a[200];


    > after I include the header file in a dot c file


    > and define the var in the global scope of the dot c file as


    > struct mytype a[200];


    > but the compiler complains the redefinition occurs,


    Are you sure it's the compiler and not the linker?

    > how to cope with it? thanks in advance.


    Having an "extern" declaration of a variable (of whatever type)
    in a header file and then defining the variable in one of the
    source files is completely legal and has been done by lots of
    people in lots of programs. So I suspect your problem is that
    you try to define the variable in more than one source file,
    i.e. that you have the line

    struct mytype a[200];

    not only in a single source (.c) file but in two or more. And
    that's, of course, something you can't do, because then you have
    two or more instances of that variable and the linker won't know
    which one you meant with the declaration in the header file.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 11, 2005
    #3
  4. baumann@pan

    CBFalconer Guest

    "baumann@pan" wrote:
    >
    > in a .h header file
    > i want to declare a var as
    >
    > extern struct mytype a[200];
    >
    > after I include the header file in a dot c file
    > and define the var in the global scope of the dot c file as
    >
    > struct mytype a[200];
    >
    > but the compiler complains the redefinition occurs,


    Where have you defined struct mytype?

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, May 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zunbeltz Izaola

    method in the definiton of a methond

    Zunbeltz Izaola, Oct 29, 2003, in forum: Python
    Replies:
    3
    Views:
    337
    Terry Reedy
    Oct 29, 2003
  2. Fatih Gedikli

    Definiton of variables within a block?

    Fatih Gedikli, Mar 22, 2005, in forum: C Programming
    Replies:
    2
    Views:
    297
    Walter Roberson
    Mar 22, 2005
  3. Replies:
    2
    Views:
    360
    Joe Wright
    Jun 4, 2006
  4. Bolin
    Replies:
    4
    Views:
    411
  5. J. Cliff Dyer
    Replies:
    1
    Views:
    406
Loading...

Share This Page