Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > variable definiton / variable declaration

Reply
Thread Tools

variable definiton / variable declaration

 
 
baumann@pan
Guest
Posts: n/a
 
      05-11-2005
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

 
Reply With Quote
 
 
 
 
baumann@pan
Guest
Posts: n/a
 
      05-11-2005
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?

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-11-2005
baumann@pan <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-11-2005
"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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to see the code definiton in the shell ? J. Cliff Dyer Python 1 05-13-2009 11:22 PM
Type definiton with structurs probem and fgets noridotjabi@gmail.com C Programming 2 06-04-2006 02:48 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
Definiton of variables within a block? Fatih Gedikli C Programming 2 03-22-2005 12:54 AM
method in the definiton of a methond Zunbeltz Izaola Python 3 10-29-2003 05:09 PM



Advertisments