1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Why can the offsetof macro not be applied to a bit-field…

Added 30 Nov -0001

Why can the offsetof macro not be applied to a bit-field member of a structure?

  1. offsetof is undefined for any member of a packed structure or union
  2. The macro requires a union, and bit-fields may appear only inside structs
  3. Bit-fields need not start on a byte boundary, so no byte offset exists Correct answer
  4. Bit-fields are not members of a structure and have no declared name

Correct answer

C. Bit-fields need not start on a byte boundary, so no byte offset exists

16 views

MCQSEXAM

Explanation

offsetof yields a size_t byte offset, but a bit-field may begin at any bit position and has no address, so it cannot satisfy the macro's requirement that the member be addressable. Applying offsetof to a bit-field is a constraint violation, not merely an unspecified result. The same lack of an address is why &s.flag is illegal for a bit-field, while &s.whole is perfectly valid.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs