Thread: Need help with basic enumeration

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    42

    Need help with basic enumeration

    So I really haven't used enumeration since my intro to c class and now have to for this program. I'm having trouble understanding how to go about using it.

    In a header file that was given to me i have:

    Code:
    typedef enum { LOGON_EVT = 0, SIO_EVT, WIO_EVT, EIO_EVT, END_EVT, TIMER_EVT, SEGFAULT_EVT, ADRFAULT_EVT, NUM_EVENTS } event_type;


    And i want to use it in a get_event_id function.

    How do I access this properly though?


    I need to compare a char* that will be the first half of each of those strings (LOGON, SIO, WIO, EIO, and so on)

    then if they are equal return the value that it represents.

    this is the best i can come up with but it gives me a syntax error before event_type:

    Code:
    for(j =0; j<NUM_EVENTS; j++)
                {
                        if(event_name == event_type[j])
                        {
                           return j;
                        }
                        else j++;
                        
                }


    also while im at it, if i just strcmp LOGON with LOGON_EVT how can i know that is a match? or should i concatenate _EVT onto each one then compare?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You refer to the individual members by name (SIO_EVT, WIO_EVT, EIO_EVT), like they were #defines. The point of an enum is to just create a sequential list of values.

    Quote Originally Posted by DaNxTh3xMaNx View Post
    I need to compare a char* that will be the first half of each of those strings (LOGON, SIO, WIO, EIO, and so on)

    also while im at it, if i just strcmp LOGON with LOGON_EVT how can i know that is a match? or should i concatenate _EVT onto each one then compare?
    No dice there. Enums are integers, not strings. You cannot use strcmp, nor can you do anything with the label at runtime (unless you create a parallel array of char*, but that would be pretty bizarre).

    I think you are confused slightly about whatever API it is you are using. If you are creating a callback handler in terms of the API, it should supply you with an int indicating the event type -- perhaps inside a structure. If you provide more information about this we can sort it out.
    Last edited by MK27; 09-05-2011 at 08:46 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    Quote Originally Posted by MK27 View Post
    You refer to the individual members by name (SIO_EVT, WIO_EVT, EIO_EVT), like they were #defines. The point of an enum is to just create a sequential list of values.

    so i would just have 9 string compares in the for loop checking for each one individually? if so then how do i return the integer value of the event just return LOGON_EVT; ?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    Quote Originally Posted by MK27 View Post
    You refer to the individual members by name (SIO_EVT, WIO_EVT, EIO_EVT), like they were #defines. The point of an enum is to just create a sequential list of values.



    No dice there. Enums are integers, not strings. You cannot use strcmp, nor can you do anything with the label at runtime (unless you create a parallel array of char*, but that would be pretty bizarre).

    I think you are confused slightly about whatever API it is you are using.


    Here is the function description:

    Capitalize event name so that case does not matter
    Verify that name's length is shorter than constant the EVENT_NAME_LENGTH_MAX
    For event ID = 0 to NUM_EVENTS
    Compare event name to Event_Names[ event ID ]
    If they are equal
    Return event ID
    Otherwise,
    Continue in loop
    Report error if no event matched
    \endverbatim


    event name is a char* and the other one is the enumeration from what i understand, guess i should just ask the professor

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by DaNxTh3xMaNx View Post
    Here is the function description:

    event name is a char* and the other one is the enumeration from what i understand, guess i should just ask the professor
    Yuck! But doing that with #defines and such is sometimes needed for human readable output, etc. Kind of tedious because AFAIK, the only way to do it is to hardcode something like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    enum {
    	ONE = 1,
    	TWO,
    	THREE
    };
    
    int main(int argc, char *argv[]) {
    	int i, len = strlen(argv[1]);
    
    	for (i = 0; i < len; i++) argv[1][i] = tolower(argv[1][i]);
    
    	if (!strcmp(argv[1], "one")) return ONE;
    	if (!strcmp(argv[1], "two")) return TWO;
    	if (!strcmp(argv[1], "three")) return THREE;
    
    	return 0;
    }
    Last edited by MK27; 09-05-2011 at 09:02 AM. Reason: whoops 0->1
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    tried this:

    Code:
       	
    	int i =0, len = strlen(event_name[1]);
    
    if (!strcmp(event_name[1], "LOGON")) return LOGON_EVT;
    	if (!strcmp(event_name[1], "SIO")) return SIO_EVT;
    	if (!strcmp(event_name[1], "WIO")) return WIO_EVT;
    	if (!strcmp(event_name[1], "EIO")) return EIO_EVT;
    	if (!strcmp(event_name[1], "END")) return END_EVT;
    	if (!strcmp(event_name[1], "TIMER")) return TIMER_EVT;
    	if (!strcmp(event_name[1], "SEGFAULT")) return SEGFAULT_EVT;
    	if (!strcmp(event_name[1], "ADRFAULT")) return ADRFAULT_EVT;
    	printf("Error: No such Event.");
    	return NUM_EVENTS;

    but every line had a warning passing arg1 of strcmp (strlen for first line) makes pointer from integer without a cast. did i do something wrong? (note i already have the words the case i need so i didn't include the for loop)

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DaNxTh3xMaNx View Post
    so i would just have 9 string compares in the for loop checking for each one individually? if so then how do i return the integer value of the event just return LOGON_EVT; ?
    You don't have access to the text names in an enum, except to use them as variable names. They work like #defines as already explained...
    Code:
    printf("%d\n", SIO_EVT);   // ok
    if (problem == SIO_EVT)    // ok
    if (string == SIO_EVT)   // not ok

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by DaNxTh3xMaNx View Post
    but every line had a warning passing arg1 of strcmp (strlen for first line) makes pointer from integer without a cast. did i do something wrong?
    Post the declaration of event_name. Sounds like event_name[1] may be a single char (which is an integer value). If you are trying to use that as a pointer into the string (so you skip the first byte) use &event_name[1].
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    42
    Quote Originally Posted by MK27 View Post
    Post the declaration of event_name. Sounds like event_name[1] may be a single char (which is an integer value). If you are trying to use that as a pointer into the string (so you skip the first byte) use &event_name[1].
    i changed it to: if (!strcmp(event_name, "LOGON")) return LOGON_EVT;

    (took out the [1] completely and it did what i wanted. so thanks for the help)

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by DaNxTh3xMaNx View Post
    i changed it to: if (!strcmp(event_name, "LOGON")) return LOGON_EVT;

    (took out the [1] completely and it did what i wanted. so thanks for the help)
    Good, because that was the correct fix. MK27 had accidentally lead you astray there.
    I can see that you got the [1] from MK27's example (where it was correct), and then he assumed it was your idea when you tried to incorrectly also use it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration
    By webren in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 02:47 PM
  2. Enumeration help
    By CXHatchback in forum C Programming
    Replies: 3
    Last Post: 04-17-2003, 08:59 AM
  3. enumeration
    By GaPe in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2003, 06:11 AM
  4. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM
  5. Enumeration in C and C++
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-27-2001, 01:12 PM