On Apr 24, 8:19 am, Charlie <evilzucch...@y ahoo.comwrote:
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
>
Thank you,
Charlie