473,323 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

newb help: making sense of typedef enum...

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;
int number_values;
} section_t;
-------------

These declarations describe the header of each of a number of sections
of a binary file (so the assignment is to use this info to read and
make sense of the data in a supplied data file).

Now, I typed this stuff into a small program and ran it though gcc.
gcc had no complaints, but I'm puzzled as to how, in the struct,
DATA_TYPE can be a valid type, since in the enum there is DOUBLE_LIST
and INT_LIST... nothing that seems like a type that a compiler would
understand without more information.

If someone could provide me with the approximate English translation
of the above statements, I'd greatly appreciate it. /Someday/ I'll be
a passable programmer =)

Thanks again.
-chris

*-also posted on .moderated, but saw that there wasn't much recent
activity there =)
Nov 14 '05 #1
4 3014
In article <93**************************@posting.google.com >
Chris <ia*******@hotmail.com> writes:
I'm trying to make sense of the following. ...
typedef enum { DOUBLE_LIST, INT_LIST } DATA_TYPE;
The typedef here is something of a red herring.

The best thing to do, as far as I am concerned, for understanding
purposes, is to remove all the typedefs from C code. In this case,
that also requires adding an enumeration tag, giving:

enum list_enum { DOUBLE_LIST, INT_LIST };

This defines a new type, "enum list_enum", with two enumeration
members. The two enumeration members are just names for integer
constants, numbered sequentially starting from 0 (in this case).

All the typedef does, once it is re-inserted, is make an alias
-- another name -- for the type "enum list_enum":

typedef enum list_enum DATA_TYPE;

Now you can type "DATA_TYPE" wherever you would otherwise have to
type "enum list_enum". (In fact, there is no requirement that the
typedef come second, and if you want to use typedefs with linked-list
structures, I recommend putting the typedef line first. I prefer
not to use typedef at all, myself, though.)
typedef struct {
DATA_TYPE type;
int number_values;
} section_t;
Here is another one of these silly mixed-up typedef lines again.
Assuming you *want* to use typedefs (for whatever strange reason),
I still claim it is better to write the typedef first, *then* the
struct declaration. Of course, you will have to add a tag, just
as before:

typedef struct section section_t;
struct section {
DATA_TYPE type; /* or: enum list_enum type; */
int number_values;
};

Again, the typedef does not really add anything -- the "real"
type is "struct section"; "section_t" is just an alias for this,
an extra name that lets you skip the word "struct".

It is worth noting here that both "section_t" and "DATA_TYPE"
are spelled specially: the former ends in "_t", and the latter
is in all-capital-letters. When using typedefs, it *is* a good
idea to mark them specially, because they kind of goof up the
syntax of C programs. Ordinary declarations will have an
instantly recognizable keyword in front:

int i;
struct S somestruct;
void f(void);
double (d); /* unnecessary parentheses, but for illustration */

But suppose you see:

zog (x);

Is this a declaration of the variable "x" of type "zog", where
"zog" is a typedef-alias for some other type? Or is it a call
to the function zog(), passing the value of x? If all your
typedefs are consistently marked, you can tell:

zog_t (x); /* this one is a variable declaration */
mog (y); /* this one is a call to a function */

In this case, one typedef uses an "_t" suffix, and the other uses
all-uppercase. It seems a bit odd to mix conventions like this.
Now, I typed this stuff into a small program and ran it though gcc.
gcc had no complaints, but I'm puzzled as to how, in the struct,
DATA_TYPE can be a valid type, since in the enum there is DOUBLE_LIST
and INT_LIST... nothing that seems like a type that a compiler would
understand without more information.


Enumeration members are just aliases for integer constants. Even
if the enumeration does not have a tag, you can even use this to
define constants -- real, true constants, not read-only "const"
variables -- that you can use wherever you would otherwise use a
constant:

enum { MAX_LEVELS = 30 };

int some_table[MAX_LEVELS];

The original enumeration did not have a tag, but its type -- an
otherwise un-nameable type -- is "captured" by the surrounding
typedef:

typedef enum /* no tag */ { MEMBER } alias;

Now "alias" is an alias for the missing enumeration tag. The
members are just integer constants, and in this case just 0 and
1 respectively. So:

if (i == INT_LIST)

means exactly the same thing as:

if (i == 1)

To declare i as a variable with the enumerated type, you might
normally write either:

enum the_tag i;

or:

alias i;

but here the tag is missing, so you have to use the typedef-alias.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #2
Chris Torek wrote:
The best thing to do, as far as I am concerned, for understanding
purposes, is to remove all the typedefs from C code. In this case,
that also requires adding an enumeration tag, giving:

enum list_enum { DOUBLE_LIST, INT_LIST };

This defines a new type, "enum list_enum", with two enumeration
members. The two enumeration members are just names for integer
constants, numbered sequentially starting from 0 (in this case).

All the typedef does, once it is re-inserted, is make an alias
-- another name -- for the type "enum list_enum":

typedef enum list_enum DATA_TYPE;

Now you can type "DATA_TYPE" wherever you would otherwise have to
type "enum list_enum". (In fact, there is no requirement that the
typedef come second, and if you want to use typedefs with linked-list
structures, I recommend putting the typedef line first.


The typedef has to come second (or combined with the type declaration)
for enum types, since C doesn't allow forward declarations of enums.

Jeremy.

Nov 14 '05 #3
>Chris Torek wrote:
... (In fact, there is no requirement that the
typedef come second, and if you want to use typedefs with linked-list
structures, I recommend putting the typedef line first.

In article <sl*******************@jita.cl.cam.ac.uk>
Jeremy Yallop <je****@jdyallop.freeserve.co.uk> writes:The typedef has to come second (or combined with the type declaration)
for enum types, since C doesn't allow forward declarations of enums.


Oops, right:

% cat t.c
typedef enum zorg zorg_t;
enum zorg { A_BIT_EVIL, SOMEWHAT_EVIL, VERY_EVIL, BLOTTO };

zorg_t obj;
% cc -ansi -pedantic -c t.c
t.c:1: warning: ANSI C forbids forward references to `enum' types

GNUC allows this sort of thing as an extension, and there is no
fundamental reason ANSI C has to forbid it, but it does.

(It also does not help that I wrote the original posting in two or
three parts, and then moved some of the parts around, between other
work-related tasks. :-) )
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
Chris Torek <no****@torek.net> writes:
Oops, right:

% cat t.c
typedef enum zorg zorg_t;
enum zorg { A_BIT_EVIL, SOMEWHAT_EVIL, VERY_EVIL, BLOTTO };

zorg_t obj;
% cc -ansi -pedantic -c t.c
t.c:1: warning: ANSI C forbids forward references to `enum' types

GNUC allows this sort of thing as an extension, and there is no
fundamental reason ANSI C has to forbid it, but it does.


It probably wouldn't be too useful to allow forward references to
`enum's. It's useful for struct and union types, because they
are often referred to through pointers, but pointers to enum
types are relatively rare. If all enums were the same width,
then forward references would be more useful, but that would be
an additional change in the language.
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein
Nov 14 '05 #5

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

Similar topics

1
by: sks_cpp | last post by:
How can I get the following to work? Are the templates defined incorrectly? I am running this on Visual C++ 6.0. // =============================================== #include <iostream> using...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
11
by: The_Kingpin | last post by:
Hi all, I'm new to C programming and looking for some help. I have a homework project to do and could use every tips, advises, code sample and references I can get. Here's what I need to do....
26
by: Alexander Farber | last post by:
Hi, I have a UNIX-server written in C, which exchanges messages with Java clients. In the message the 1 byte is its length, the 2nd a player number and the 3rd byte is an event id: /* 0....
3
by: you | last post by:
Ok, I hope that I can 'splain this right. I am very new so bear with me. I have a treeview with its main node being a user selected folder. In this treeview you can only see subfolders and two...
10
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
I came over this code which puzzled me. Isn't the enum supposed to have an identifier? enum { BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ BT_OPEN, BT_BOUND,...
6
by: dillogimp | last post by:
hi This code is taken from a lisp interpreter by Andru Luvisi. typedef struct obj { enum otype type; struct obj *p; // array of pointer, sizeof(obj)=8 } obj; typedef obj * (*primop)(obj *);...
8
by: abhradwip | last post by:
I want to write a program which will find the shortest path between n no. of cities using dijkstra's algorithm ...... but could not do it..... i have written a program which will give the shortest...
4
by: msukumarbabu | last post by:
Hi all, What will be difference between "typedef enum" and "enum". or difference between “typedef structure" and "structure" I am going through some code. in that some place they are using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.