extern and enum typed variables

C

Charlie

I use a file to define all of my global variables, data.c, and then I
create a header file, data.h, where I reference all the variables with
the keyword "extern." I include the header in every file.

I define an enum and variable of that type in data.c,

enum myenum
{
val1,
val2,
val3
} enum_var;

When I try to reference the variable, enum_var, in data.h the compiler
does not like it.

I have tried multiple ways of referencing it but I fail each time so
there is clearly something I do not understand about enum or extern or
god knows what.

So I guess to sum up my question is how do I reference the variable
enum_var so that I can include it in each file.

All help would be appreciated.

Thank You,

Charlie
 
C

Chris Dollin

Charlie said:
I use a file to define all of my global variables, data.c, and then I
create a header file, data.h, where I reference all the variables with
the keyword "extern." I include the header in every file.

Good stuff. (You're not /referencing/ the variables; you're /declaring/
them, saying that they exist.)

Note: /don't use global variables unless you have to/. They're an
easy way to get your kickers in a twinge. You know the way egg-white
is nice and fluid before cooking, but cooking it -- cross-connecting
all those protein strands -- makes it, if not actually /hard/, much
stiffer and not a fluid? Global variables are like that, they make
it harder (and harder and harder) to rearrange your program as time
goes by and each variable cross-connects to more and more bits of
code until your trapped in a maze of twisty little codelines all
almost, but not quite, completely different.
I define an enum and variable of that type in data.c,

enum myenum
{
val1,
val2,
val3
} enum_var;

That enum isn't visible in `data.h` and so not to anything that includes
it.
When I try to reference the variable, enum_var, in data.h the compiler
does not like it.

Good compiler, here's a biscuit.
I have tried multiple ways of referencing it but I fail each time so
there is clearly something I do not understand about enum or extern or
god knows what.

So I guess to sum up my question is how do I reference the variable
enum_var so that I can include it in each file.

All help would be appreciated.

Put the declaration in the header file, same as you do for all the
other variables.
 
P

pete

Charlie said:
I use a file to define all of my global variables, data.c, and then I
create a header file, data.h, where I reference all the variables with
the keyword "extern." I include the header in every file.

I define an enum and variable of that type in data.c,

enum myenum
{
val1,
val2,
val3
} enum_var;

When I try to reference the variable, enum_var, in data.h the compiler
does not like it.

I have tried multiple ways of referencing it but I fail each time so
there is clearly something I do not understand about enum or extern or
god knows what.

So I guess to sum up my question is how do I reference the variable
enum_var so that I can include it in each file.

All help would be appreciated.

It's OK to define typedefs and enum types in a header file.
Not OK for objects and functions

This would be OK in a header file:
enum myenum {
val1,
val2,
val3
};
 

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

Similar Threads

enum types and extern 10
enum variables and extern 9
enum 6
extern variables 8
Compile time details of an 'extern variable' 2
Extern variables and header files 8
"extern" meaning 19
Use of 'extern' keyword 5

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top