Thread: looking to use sizeof for enum?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    looking to use sizeof for enum?

    I have a type enum and would like to use the sizeof command to get a value....Can I do this? For instance my enum is:

    Code:
    typedef enum {EMPTY, RST, PWM_DIV, PWM_DUTYCYCLE, DAC_REF, ADC_SAMP_TIME = 8, ADC_NUM_SAMPLES, ADC_CONV} commands
    I would like to do sizeof(commands) and get back 8....if I run as is I get back 2 which presumably is the size of a type enum

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    sizeof won't do what you want since commands is a type rather than some kind of aggregate. Since you're explicitly assigning a value in the middle, you cannot use the trick of computing from the value of the last enumerator either. Why do you need to know this count? Maybe there is another way that does not rely on such knowledge.
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you don't mess up the numbering with an assignment, you typically do this.
    Code:
    typedef enum {EMPTY, RST, PWM_DIV, PWM_DUTYCYCLE, DAC_REF, ADC_SAMP_TIME, ADC_NUM_SAMPLES, ADC_CONV, CMD_LAST} commands
    CMD_LAST will be the number of elements that precede it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by ridgerunnersjw View Post
    I have a type enum and would like to use the sizeof command to get a value....Can I do this? For instance my enum is:

    Code:
    typedef enum {EMPTY, RST, PWM_DIV, PWM_DUTYCYCLE, DAC_REF, ADC_SAMP_TIME = 8, ADC_NUM_SAMPLES, ADC_CONV} commands
    I would like to do sizeof(commands) and get back 8....if I run as is I get back 2 which presumably is the size of a type enum

    Thanks
    From what I see I think you're misunderstanding what sizeof does, for starters sizeof(commands) will, in general, be equal to sizeof(int), that is the sizeof of what will contain the values of your enums, not the count of them. Secondly to get the count you could do something as simple as this:

    Code:
    typedef enum
    {
    	EMPTY = 0,
    	RST,
    	PWM_DIV,
    	PWM_DUTYCYCLE,
    	DAC_REF,
    	ADC_SAMP_TIME = 8,
    	ADC_NUM_SAMPLES,
    	ADC_CONV,
    	LAST_CMD_MARKER,
    	CMD_COUNT = DAC_REF + (LAST_CMD_MARKER - SAMP_TIME)
    } commands;
    Might be 1 off with the count, I didn't bother with a math check since it was off the top of my head, can just add - 1 to the end if it is, anyways CMD_COUNT would then hold what you were looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe with enum. Assigning a char to an enum value.
    By Iceboxes in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2013, 09:58 AM
  2. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 02:32 AM
  3. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  4. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  5. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM

Tags for this Thread