Thread: Can i define structure like that ?

  1. #1
    Registered User
    Join Date
    Mar 2010

    Can i define structure like that ?

    Hello all,

    I come up with a strange defination of a struct ?
    Can we define it like that.
    And what does it means also how to solve that type of problem ?
    here is the code
    struct value
            int bit1:1;
            int bit3:4;
            int bit4:4;
        }bit={1, 2, 13};
        printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    i think it suppose to print value as 1,2,13 actually it print value as -1,2,-3
    How this happen ?

    Please clerify it as this is totally new type to me..!!


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > int bit1:1;
    How many sign bits do you have in a signed integer.
    How many value bits do you have left?
    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
    Join Date
    Aug 2010
    Ontario Canada
    try it like this....
    struct value
      { unsigned int  bit1 : 1,
                      bit3 : 4,
                      bit4 : 4; }
      bit = {1,2,13};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detailed ASCII Pong Game, please review!
    By parad0x13 in forum Game Programming
    Replies: 5
    Last Post: 08-02-2010, 10:45 PM
  2. DHCP Discover
    By Xandux in forum C Programming
    Replies: 4
    Last Post: 03-06-2010, 12:41 AM
  3. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 01:34 PM
  4. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  5. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM