Re: confused about extern use



Hi all,
I really appreciate all answers relating to the post particularly
of Mr Nick Keighley.
it is very lucid to understand. And I am sorry for using
abbreviations like "u".
But still I have some doubt. I read about extern that whenever we
need to use the same global
variable across different C-files we need to use extern keyword
and declare it so that it will
refer to same variable where it got defined.
So the above style ( whatever Nick have given as example) can be
re-written like define the
structure in the header-file. i.e
just struct abc {
int a;
int b;
};
Then define a variable of the structure in the required C-
file and in above case in file-1.c
as : struct abc abc_var1; //as a global variable. Then If u
want to use it in another C-file
file-2.c we can sdeclare as: extern struct abc abc_var1; //It
will refer to the abc_var1 of file1.c

I can understand that whatever Nick explained the same thing will
happen (If I am not wrong)
In the preprocessing operation header file will populate the c-
file with the extern declaration.
But I think in file-1.c the extern declaration and the defination
of variable both will apear
after preprocessing header file. i.e

extern struct abc abc_var1
struct abc abc_var1
Is this not a problem.Though not a error but We can avoid this
by define the structure in a header file and then define a variable of
the structure type in a c-file then use extern keyword to refer to the
same variable wherever we want. This solves the whole purpose what we
can achieve out of
doing in the manner Mick replied or i have posted in the querry.

Thanks
Das



.



Relevant Pages

  • Re: confused about extern use
    ... going to achieve by declaring it as extern in the header file a.h. ... composed on one or more compilation units. ... Essentially you *declare* the type wherever it is ...
    (comp.lang.c)
  • Re: How to debug Link errors of static LIB in eVC?
    ... extern "C" { ... directory of the header file which I had set. ... but your compiler finds somehow the C++ prototype of the function and generates the decorated name for the unresolved external. ... PPlugin_PSoundChannel_WAVFile_Registration_Instance that returns a PPlugin_PSoundChannel_WAVFile_Registration class object. ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: alpha defined: virtualcnt,titleid,keyid,helplineid,pasteboardid
    ... > Where would I best find EXTERN and GLOBAL variables? ... Going back to the C sources: the definitions and (EXTERN) ... thus finding the header file. ... Another way would be to use the CTAGS program, which keeps trace ...
    (comp.os.vms)
  • Re: Getting function signature
    ... source code (or at least a declaration in a header file). ... if the function you're looking to call was declared as "extern ... name mangling is turned off (and you can't overload that ...
    (comp.lang.c)
  • Re: c++ calling c functions
    ... the 'extern "C" ' wrapper. ... separate .h header file, with the same wrapper, for use only in the ... The last one makes it trivial to mark large blocks of code as having a particular language linkage. ... You do not need separate header files for C and C++. ...
    (comp.lang.c)