0

I am using a file shared between multiple programmers which has this struct:

typedef struct _APPLE{
   ULONG appleID;
   struct{
       ULONG isBig: 1;
       ULONG isRed: 1;
       ULONG isFresh: 1;
       ULONG isGood: 1;
       ULONG bReserved: 28;
   };
}APPLE;

As the file is shared I cant edit it.I want to use this APPLE struct in my code and want to provide values to each of its members. How can I do this?

3
  • What is the issue you are facing in creating its instances? Is there any problem in doing Apple a; a.isBig=0;? Commented Nov 22, 2013 at 5:43
  • I can do that. but when i do this a.isBig=0; does the memory is shared like a union here or i can give values for each of the member separately? Commented Nov 22, 2013 at 5:49
  • No. Its your own object created. Commented Nov 22, 2013 at 5:53

2 Answers 2

2

First of all you can't have anonymous nested structures in standard C, it's an extension used by some compilers.

So you have to name your bit-field structure:

typedef struct _APPLE{
   ULONG appleID;
   struct{
       ULONG isBig: 1;
       ULONG isRed: 1;
       ULONG isFresh: 1;
       ULONG isGood: 1;
       ULONG bReserved: 28;
   } flags;
}APPLE;

Then just use normal dot-notation to access the fields:

APPLE apple;
apple.appleID = 5;
apple.flags.isBig = 1;
apple.flags.isRed = 0;

And while multiple members of a bit-field may share the same int, they are still separate from each other. So changing one member of a bit-field will not change any other.

Sign up to request clarification or add additional context in comments.

1 Comment

I am using Visual Studio 2012, i think its supported here. becoz other programmers are using it.
0

According to ISO/IEC 9899:2011, section §6.7.2.1 Structure and union specifiers

An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.