Can i define structure like that ?

This is a discussion on Can i define structure like that ? within the C Programming forums, part of the General Programming Boards category; Hello all, I come up with a strange defination of a struct ? Can we define it like that. And ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    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
    Code:
    main(void)
    {
    
    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..!!

    Thanks,
    Gunjan

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    try it like this....
    Code:
    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-05-2010, 11:41 PM
  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

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