Thread: strange behavior using enum

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    strange behavior using enum

    Hi all,

    I specify an enum which I use in another struct as show below:

    Code:
    typedef enum {
    	L_R,		
     	L_I,		
     	L_L
    }S_LVL;
    
    typedef struct abc{
        int mem_a;         
        long mem_b;				
        long mem_c;
        long mem_d;
    
        /*so on, and ... */
       S_LVL lvl;					
    }abc_t, *abc_ptr;
    Later in my code, I am instantiating the structure abc_t. Then, I add values to its members, The problem is no matter, if I assign
    Code:
     
    abc_t something;
    something.lvl = L_R;
    or
    Code:
     
    something.lvl= L_I;
    or
    Code:
    something.lvl = L_L;
    my outputs always showing that it was, for example:
    Code:
     L_R
    assigned.
    What is the reason, what I may be doing wrong here ....

    thanks all.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I tested with this program:
    Code:
    #include <stdio.h>
    
    typedef enum {
        L_R,
        L_I,
        L_L
    }S_LVL;
    
    typedef struct abc {
        int mem_a;
        long mem_b;
        long mem_c;
        long mem_d;
    
        /*so on, and ... */
        S_LVL lvl;
    }abc_t, *abc_ptr;
    
    
    int main(void) {
        abc_t something;
        something.lvl = L_I;
        printf("L_R=%d\nsomething.lvl=%d\n", L_R, something.lvl);
        return 0;
    }
    My output was:
    Code:
    L_R=0
    something.lvl=1
    so clearly you are mistaken about L_R always being assigned.

    By the way, do not typedef struct abc* to be abc_ptr. Just use abc_t* instead as the pointer typedef does not add any value over actual pointer syntax.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi, To add further, I have the following macros that give access to struct members. For example,

    Code:
    #define abc_Get_level(_p_)   ( (_p_)->lvl)
    It is this macro which gives always the same value i.e. L_R

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the error. I have already proven to you that there is no problem by posting a small and simple program that demonstrates that there is no problem. If you disagree, prove it by posting your own small and simple program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    thank you, I made a small program but its working fine, as expected, my original program is complex, I will have to see, where does the problem happen

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zahid990170
    I made a small program but its working fine, as expected, my original program is complex, I will have to see, where does the problem happen
    That is good, because then you have a hint that the problem probably lies elsewhere. Furthermore, if necessary, you could rewrite that part starting from this small piece of code that you know works fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Quote Originally Posted by laserlight View Post
    That is good, because then you have a hint that the problem probably lies elsewhere. Furthermore, if necessary, you could rewrite that part starting from this small piece of code that you know works fine.
    thanks,

    I still could not solve my problem, However, I observed the following:

    Imagine, I create an structure variable and fill its member with values.

    I have a function defined as following:

    Code:
    signed char toCheck_a(abc_t something)
    {
        printf("(toCheck) here is %d .... \n", something.s_lvl);
        return 0;
    }
    
    signed char toCheck_b(S_LVL def)
    {
        printf("(toCheck) here is %d .... \n", def);
        return 0;
    }
    
    int main()
    {
        abc_t something;
        something.lvl = L_I;
    
       toCheck_a(something);
       toCheck_b(something.lvl );    
    
        return 0;
    }
    Now, toCheck_b returns 1. which is correct, but toCheck_a returns 0. Please note that, in a separate small program, the problem does not happen, it happens only in my complex program, ...., but I do not see, why it is happening, as I am not changing anything. As soon as, the function has to receive as an argument, an structure object or pointer to a structure object, it just gives me wrong results.

    I am not sure, if you would have any insights to this.

    thanks for your time,

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I might guess that you have some out of bounds access causing memory corruption.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange program behavior
    By milli-961227 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2015, 11:57 AM
  2. Need help, please - very strange behavior
    By snork in forum C Programming
    Replies: 16
    Last Post: 09-26-2011, 01:36 AM
  3. Strange behavior
    By onako in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 07:00 AM
  4. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  5. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM