Struct with pre-defined constants, possible?

This is a discussion on Struct with pre-defined constants, possible? within the C Programming forums, part of the General Programming Boards category; Is it possible to make a struct that have some members pre-defined and non editable? Letīs say I make a ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485

    Struct with pre-defined constants, possible?

    Is it possible to make a struct that have some members pre-defined and non editable? Letīs say I make a struct with 5 members, where only 2 is possible to asign values to while the other three is predefined. I have tried in various ways like using constants in the struct declaration without success.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,769
    I suspect that it is not possible in C, since the natural solution in C++ would be to use a constructor initialisation list, which is a feature not available in 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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could do something like this:
    Code:
    struct allmembers
    {
        const struct constmembers
        {
            int x;
            int y;
        } consts;
        int z;
        int w;
    };
    
    int main()
    {
      struct allmembers all = { { 3, 2 }, 0, 0 };
    
      all.consts.x = 1;  /* Error compiling.  */
      all.z = 5;
    
      return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,769
    The best I have been able to come up with is to place the const member variables first and then use an initialiser... but that still does not fully satisfy the "pre-defined" requirement:
    Code:
    struct X
    {
        const int a;
        const int b;
        int c;
    };
    
    int main(void)
    {
        struct X x = {1, 2};
        x.c = 3;
        return 0;
    }
    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

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks, it looks like I would need to initialize manualy. What Iīm thinking about is a way to deal with data packets where some members are defined as a way to give identity to them. This is the struct I had in mind:
    Code:
    	typedef struct {
    		short		header;			// F07E			
    		char		channelNumber;
    		char		id;			// 02
    		char		data[120];			
    		short		cheksum;			
    		char		end;			// F7						
    	} DataPacket;
    The header, id and end should stay the same while channelNumber, data and checksum should be variables. I guess that I could make a loop that updates the variables and give the other members constant values.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,769
    You could define say, an initDataPacket() function that is supposed to act as a constructor.
    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

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks laserlight, that seems like a good solution Iīll stick to that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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