enum redeclaration

This is a discussion on enum redeclaration within the C Programming forums, part of the General Programming Boards category; I would have liked to do this: Code: typedef enum { Exp = 0x14110, Gain = 0x14150, } Type_Device1_RegisterAddress; typedef ...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    enum redeclaration

    I would have liked to do this:

    Code:
    typedef enum {
         Exp = 0x14110,
         Gain = 0x14150,
    } Type_Device1_RegisterAddress;
    
    typedef enum {
         Exp = 0x00110,
         Gain = 0x00150,
    } Type_Device2_RegisterAddress;
    but i receive compiler errors
    how would you suggest to dio something similar?

    i don't like much the following (cause it's something close to using defines):

    Code:
    typedef enum {
         Device1_Exp = 0x14110,
         Device1_Gain = 0x14150,
    } Type_Device1_RegisterAddress;
    
    typedef enum {
         Device2_Exp = 0x00110,
         Device2_Gain = 0x00150,
    } Type_Device2_RegisterAddress;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Would you like to use C++ instead? I do not think that C provides such a scoping mechanism, but I could be wrong since I am too used to what is allowed by C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    thank you but i am working on an embedded platform that supports only alpha C++ compiler so i prefer C

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In C, all the enum descriptors are in one namespace, so it's not possible to reuse names.

    I suppose it's going to depend on how you want to use these things whether explicit names like you have it will work or not. If you always know, when you're typing the variable name, whether you need device 1 or device 2, what's wrong with it?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I don't think this is possible in C either. But maybe a datastructure like this is better:

    Code:
    const struct {
    	int exp;
    	int gain;
    } devices[] = {
    	{ 0x14110, 0x14150 },
    	{ 0x00110, 0x00150 },
    };
    And then use "devices[0].exp".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 10:16 PM
  2. enum [tag] [: type] Is this non-standard?
    By HyperShadow in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2007, 10:29 PM
  3. enum switchcase and windows message q
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 11-27-2006, 01:16 PM
  4. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 04:51 AM
  5. enum
    By JerryL in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2004, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21