FidoNet Echomail Archive
c_echo

<<< Previous Index Next >>>

From: Jonathan Guthrie
To: All
Date: 2005-01-14 14:34:56
Subject: Re: [C] Passing an enumerated data value

From: Jonathan Guthrie <jguthrie{at}brokersys.com>

On Fri, 2005-01-14 at 13:46, TLewis{at}mcleodhealth.org wrote:
> I want to pass an enumerated data type to a function.  I have code below
> that works, but I want to know if it is "proper" coding.  I
tried using
> "ENUM" in my function arguments, but that does not work.
>
> Thanks,
> Tim
>
> #include <stdio.h>
> void ShowPosition(int);
>
> enum POSITION{TOP,BOTTOM};
>
> main() {
> printf("\nMain routine");
>
> ShowPosition(TOP);
>
> }
>
> void ShowPosition(int POSITION) {
>
> if (POSITION == TOP)
>     printf("\nPostion is top");
> if (POSITION == BOTTOM)
>     printf("\nPosition is bottom");
> }

I don't know what you mean when you say that you "tried using 'ENUM'
in my function arguments, but that does not work."  I turned your
program into this program:
#include <stdio.h>

enum POSITION{TOP,BOTTOM};
void ShowPosition(enum POSITION);

main() {
        printf("\nMain routine");

        ShowPosition(TOP);

        return 0;
}

void ShowPosition(enum POSITION POSITION) {
        if (POSITION == TOP)
                printf("\nPostion is top");
        if (POSITION == BOTTOM)
                printf("\nPosition is bottom");
}


This program compiles and runs correctly when I use gcc V3.3.5 on my main
development computer.

The changes I have made are these:  Move the declaration of ShowPosition
until after the definition of the "enum POSITION" type.  Update
the type of the declared argument to ShowPosition from int to "enum
POSITION". Add a return value from main, because main is declared to
return an int value.  Update the declaration of the POSITION argument to
ShowPosition's definition to be "enum POSITION" rather than int. 
Note that "enum POSITION" is the type.  Although this works
(because variable names are in a different name space from the name space
that holds the names of enums) it would be better for the name of the
argument in ShowPosition to be called something else, like p.

Note that many people (including myself) like using typedefs and doing something like

"typedef enum POSITION{TOP, BOTTOM} position_t;", and then using
type type "position_t" everywhere where "enun POSITION"
goes in the program I modified above.  The "_t" suffix in this
case means that it's a type, so I would read that aloud as "position
type".  I can re-write your code in that style, if you'd like, but I'd
also want to do things like move the definition of ShowPosition to before
main so that I don't have to explicitly declare it and whatnot.  You also
might want to consider specifying values for top and bottom (and, I
presume, left and right) so that they can be used as bitmasks, but that
presupposes some things about your application.

--- BBBS/LiI v4.01 Flag-5
 * Origin: Prism's_Point (1:261/38.1)
SEEN-BY: 633/267 270
@PATH: 261/38 123/500 106/2000 633/267


<<< Previous Index Next >>>