Thread: Struct with pre-defined constants, possible?

  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
    28,413
    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.
    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
    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
    28,413
    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;
    }
    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
    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
    28,413
    You could define say, an initDataPacket() function that is supposed to act as a constructor.
    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
    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, 04:05 PM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05: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, 10:24 AM