Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Pointers to bit fields in C

Back to Forum: Tech Off
  • User profile image

    A quick question I can't seem to work out, is there a way to pass a bit field to a function in C, for example I have something like this:

    struct TIME_FLAGS  {
       unsigned cent:1;
       unsigned year:1;
       unsigned month:1;
       unsigned day:1;
       unsigned hour:1;
       unsigned minute:1;
       unsigned second:1;
    } time_flags;

    and want to do this

    INT8 StoreIntElement (int *value, unsigned *flag:1)
       *value = atoi (elementBuffer) ;
       //Set flag
       *flag = 1 ;

    main ()
       INT8 value ;

       //Do stuff
       StoreIntElement (&value, &time_flags.hour) ;


    Anyone got any idea how to make this work?



  • User profile image
    Sven Groot

    I don't think you can do this. A bit field is implemented basically as a series of shift operations used to access the correct bits (which is also why you should use them carefully, yes they use less memory, but they can be a lot slower to access).

    In order to take a parameter like that, the function would have to know not only the size, but also which bit to change. This is something that's simply not supported by the language.

  • User profile image

    Thought that was the case, thanks for taking the time to answer anyway!

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.