Using extern

P

Praveen

Hi,

I would like to know is it possible not to use extern in a program.
For eg I have the following programs

file a.c contains a variable declaration as follows
bool a = TRUE;

I have declared the variable a as extern in the header file a.h as
follows
extern bool a;

In another file b.c which has included the header file a.h I try to
set the variable to FALSE;

I would like to know is there a way of not to use the extern
declaration and still achieve the above scenario.

Thanks in advance
 
C

Chris Dollin

Praveen said:
I would like to know is it possible not to use extern in a program.
For eg I have the following programs

file a.c contains a variable declaration as follows
bool a = TRUE;

(Where did your boolean type and value come from? If that `bool`
is C99, ten shouldn't `TRUE` be `true`?)
I have declared the variable a as extern in the header file a.h as
follows
extern bool a;

In another file b.c which has included the header file a.h I try to
set the variable to FALSE;

How do you do this? Did you suceeed?
I would like to know is there a way of not to use the extern
declaration and still achieve the above scenario.

Why on Earth would you not want to use the provided mechanism
for allowing a compilation unit to refer to variables defined
in a different unit?

What are you actually trying to /do/?
 
S

santosh

Praveen said:
Hi,

I would like to know is it possible not to use extern in a program.
For eg I have the following programs

file a.c contains a variable declaration as follows
bool a = TRUE;

If this is the bool defined in C99 then it can only be set to 'true'
or 'false'. All three macros, (yes bool is actually a macro) are
declared in stdbool.h.
I have declared the variable a as extern in the header file a.h as
follows
extern bool a;

In another file b.c which has included the header file a.h I try to
set the variable to FALSE;

Set it to false.
I would like to know is there a way of not to use the extern
declaration and still achieve the above scenario.

Thanks in advance

Why do you want to battle with a language? If you don't like the
mechanisms provided by C, then pick another language.

PS. By receiving a pointer to 'a' from the translation unit where it's
defined, it's possible to do what you want.
 
M

mark_bluemel

Hi,

I would like to know is it possible not to use extern in a program.

I assume you want to access data in separate compilation units,
without using extern to share the data.

The answer is "yes" of course. It depends whether you are happy with
the alternatives.

The two approaches which come to mind are either
a) provide a function in the "owning" module which returns the address
of the item you want to access
b) provide more controlled access using accessor functions - one to
get the value of the item, one or more others to set/increment/
decrement/... the value.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top