LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Find the Error · Question 4

Error: taking the address of a bit-field Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

struct status {
    unsigned ready : 1;
    unsigned error : 1;
};

void set(unsigned *flag) { *flag = 1; }

int main(void) {
    struct status s = {0};
    set(&s.ready);
    printf("%u\n", s.ready);
    return 0;
}
  • A &s.ready – the address of a bit-field cannot be taken Correct
  • B set() must take an int
  • C Bit-fields cannot be initialised with {0}
  • D There is no error; it prints 1
Correct Answer: Option A

A bit-field may start in the middle of a byte, so it has no address of its own. Set it directly (s.ready = 1;) or pass a pointer to the whole struct.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Popular