Thread: how to create an array of enum data type?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    Question how to create an array of enum data type?

    Hi

    I want to create an array of my own defined data type. Does anyone have idea how to do that: Example
    User defined data type is:

    Code:
    typedef enum{ 
    CHIP, 
    BOUNDARY, 
    BOARD, 
    CHAIN 
    } test_dft;
    Now I want to declare array of my data type. Example:

    Code:
    test_dft mode1[2] = {CHIP, CHAIN};
    I am declaring the array as above. I know that by default the enum data type is integer. First question is that, Is it ok to declare an array like this? My another question is that how would my compiler knows that the array I declared corresponds to the exact value as shown in the user defined data type? example CHAIN defined in array corresponds to the value of CHAIN in the user defined data type.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you tried it?

    Everything seems OK so far.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Quote Originally Posted by debugg View Post
    My another question is that how would my compiler knows that the array I declared corresponds to the exact value as shown in the user defined data type? example CHAIN defined in array corresponds to the value of CHAIN in the user defined data type.
    Because you declare the array "model" to contain elements of type "test_dft". Check it for yourself and add init. values not defined by your enumeration and you'll see the compiler complains.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    well I am still running it as I am running it in a big project but I still didnt know if my how my compiler knows that the array I declared corresponds to the exact value as shown in the user defined data type? example CHAIN defined in array corresponds to the value of CHAIN in the user defined data type. Does my compiler knows automatically? I am asking this question because I declared the array in different class and my user defined data type is defined in different class.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Quote Originally Posted by debugg View Post
    my user defined data type is defined in different class.
    I'm not sure to follow you... your type should be defined in only one place.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > well I am still running it as I am running it in a big project but
    But why haven't you bothered to create a separate small project with a 10-line program to test your ideas?

    Code:
    int foo = 1;
    int bar[] = { 1, 2 };
    if ( 1 == 1 )
    if ( foo == 1 )
    if ( bar[0] == 1 )
    All works the same as
    Code:
    test_dft foo = CHIP;
    test_dft bar[] = { CHIP, CHAIN };
    if ( CHIP == CHIP )
    if ( foo == CHIP )
    if ( bar[0] == CHIP )
    > I am asking this question because I declared the array in different class and my user defined data type is defined in different class.
    Regardless of where you declare it, the compiler will still need visibility of it (via say a header file), and then everything will line up correctly.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    10
    Thanks a lot everyone. It worked for me..

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    FYI,
    Code:
    typedef enum{
    CHIP,
    BOUNDARY,
    BOARD,
    CHAIN
    } test_dft;
    This is the old C way.
    Code:
    enum test_dft
    {
    	CHIP,
    	BOUNDARY,
    	BOARD,
    	CHAIN
    };
    This is the C++ way of declaring enums.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That's misinformation Elysia. C does not require enums to be typedef'd. I do have doubts that was the point. No matter what your point actually was, though, it compiles either way.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I didn't say it didn't compile. I said it was the old C way, and I said that because you had to do that or do a typedef later on to avoid having to put "enum" before the type name, but that's not required in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-17-2011, 08:53 PM
  2. create new data type
    By macroasm in forum C Programming
    Replies: 15
    Last Post: 05-18-2009, 05:46 AM
  3. Why Array is derived data type ?
    By forumuser in forum C Programming
    Replies: 2
    Last Post: 10-19-2007, 06:01 AM
  4. Array own data type?
    By TriKri in forum C Programming
    Replies: 2
    Last Post: 07-16-2006, 01:03 PM
  5. Create data type
    By Malefaust in forum C Programming
    Replies: 2
    Last Post: 09-30-2004, 06:06 AM