enum types and extern

C

Charlie

I tried to post this before and I apologize if I am repeating myself,
but I do not see the post anywhere.

But anyway,

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:

enum myenum
{
val1,
val2,
val3,
} enum_var;

When I try to reference enum_var in data.h I always get an error. I
have tried numerous ways of referencing it and defining it but I
always seem to get a compiler error.

So how can I externally reference enum_var in data.h so that I can use
enum_var in my program? And if I am totally missing the point on
this, if someone could clear up my confusion about enum or extern that
would be great.

Thank you,

Charlie
 
M

Mark McIntyre

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:
When I try to reference enum_var in data.h I always get an error.

Move the definition of the enum to your header, put the variable in
the source file and the extern reference in the header, below the enum
definition.

By the way, globals are generally considered a bad idea in C.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

Chris Dollin

Most natural numbers are greater than 17, despite the existence of
5, 11, and 15.

In practice, most natural numbers are 1.[/QUOTE]

One application global variable does sound about right.

--
"Our future looks secure, but it's all out of our hands." /Man and Machine/
- Magenta

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
R

Richard Heathfield

Chris Dollin said:
One application global variable does sound about right.

I'd consider 1 to be an upper bound. On average, it should be less than
that.
 
C

Chris Dollin

Richard said:
Chris Dollin said:

I'd consider 1 to be an upper bound. On average, it should be less than
that.

My last chunk of C [about 20K code+comments] seems to have
8 (according to a makeshift grep of the header files).

Three are equivalents of std[in,out,err], one's a debugging flag,
one's an allocation pool, one's a debugging flag, one's a title
string, and the other two are names/formats of shortcodes.

So seven of them are really constants (ie, they're set up once
and not changed thereafter), of which at least two could be
put behind functional bars.

Not too bad: could do better. (Yes, They could all be wrapped
inside one [pointer to] state variable.)
 
C

Chris Dollin

Chris said:
Three are equivalents of std[in,out,err], one's a debugging flag,
one's an allocation pool, one's a debugging flag, one's a title
string, and the other two are names/formats of shortcodes.

Oops, sorry about the echo echo.
 
K

kar1107

I tried to post this before and I apologize if I am repeating myself,
but I do not see the post anywhere.

But anyway,

I have a file, data.c, where I define all of my global variables. I
then use the extern keyword to reference those variables from a header
file, data.h, which I include in every file.

I am defining an enum type and variable in data.c:

enum myenum
{
val1,
val2,
val3,
} enum_var;

When I try to reference enum_var in data.h I always get an error. I
have tried numerous ways of referencing it and defining it but I
always seem to get a compiler error.

So how can I externally reference enum_var in data.h so that I can use
enum_var in my program? And if I am totally missing the point on
this, if someone could clear up my confusion about enum or extern that
would be great.

You define types (like enum, struct) in a header file. Typically you
do not
define such things in a .c file (unless it's self containted in the
same file
and it's not needed outside the .c file). The header file is included
in all .c files that need it.

Variables are defined in .c file (these lead to actual object creation
-- storage
allocation etc) -- you don't define variables in a header file.

Thus in your case, you both define a type and define a variable in the
same place --
and it's in the .c file. Split them into two as described above.

In data.h:
enum myenum
{
val1,
val2,
val3,
} ; <--- just defines a type -- so object/storage
allocation happens.

extern enum myenum enum_var; <-- just says somewhere in some .c file
object will be created

In data.c:
enum myenum enum_var; <--- leads to object creation

Sometimes, it helps to use a typedef for the first type definition
(typedef enum {...} myenum_en;)

Karthik
 

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


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top