Thread: struct and varialbe

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Lightbulb struct and varialbe

    Hi,
    I'm not very used to the C syntax, so I'm having this probably rather easy to solve problem for someone with experience.
    I have a struct that is of the form

    Code:
    typedef struct MyStruct{
        uint32_t myVar:2;   
    } MyStruct;
    As far as I know the :2 means that I have a myVar that has actually a width of two unit32_t. Is that correct?
    Question is, how how can I assign values to one of the 2 single instances of uint32_t?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    As you have it myvar is only 2 bits wide.
    If you want two integers you need myvar[2];
    Last edited by CommonTater; 09-03-2010 at 03:50 AM. Reason: typo

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Oh ok, thanks.
    So I can just assign limited values to the integer if its of the form above? (In this case from 0 - 3 ?)

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Yes that would be values( 0 - 3 ) for unsigned.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    But remember that your struct is actually using an unsigned int to save the first 2 bits,
    therefore you're using up memory for no reason ( 30 bit remain unused )
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by zu1u View Post
    Hi,
    ... I have a struct that is of the form
    Code:
    typedef struct MyStruct{
        uint32_t myVar:2;   
    } MyStruct;
    ...
    In C99, the only types guaranteed to be supported for bit fields are _Bool, signed int, or unsigned int. Other types may be supported as implementation-defined.

    Quote Originally Posted by Sipher View Post
    But remember that your struct is actually using an unsigned int to save the first 2 bits,
    therefore you're using up memory for no reason ( 30 bit remain unused )
    How do you know the structure will be 32-bits wide? Why could it not be 16-bits or 64-bits or some other width?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because the type is 32 bits in this example (uint32_t).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Tags for this Thread