473,770 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extern and enum typed variables

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

Apr 24 '07 #1
2 4764
Charlie wrote:
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.

--
The second Jena user conference! http://hpl.hp.com/conferences/juc2007/

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

Apr 24 '07 #2
Charlie wrote:
>
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
};

--
pete
Apr 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2594
by: John Ratliff | last post by:
I have a few global variables in my app. They are defined in the main application class file and declared (extern) in a header which can be included by anyone who would want to use these variables. Two of the globals are pointers which are initialized at runtime. The others are classes whose values are initialized in the global space. When I had just the pointers, I didn't need to include the extern header in the defining class file. But...
4
3055
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
19
3860
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
17
4933
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
3
16077
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
16
2321
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is beginning to get out of hands if I continue to put in extra functionality in the (file-writing) output-functions....
10
3037
by: Charlie | last post by:
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:
12
22762
by: Charlie | last post by:
I have a file, data.c, where I define all of my global variables. I then have a header file, data.h, which I include in every file in which I reference all of the variables defined in data.c. For the most part, I am having no problem with this. However, I defined an variable of type myenum in data.c: enum myenum {
3
2286
by: Chris Cahoon | last post by:
Hello, I am trying to include a header file which was written in C, but my project is in C++. Even when I wrap the include statement or the entire header files with extern "C" (used according to the instructions on c++-faq-lite), I get the same error, which is this: invalid conversion from `int' to `DmtxDirection' where DmtxDirection is an enum with integer values inside (obviously).
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.