ProgrammingHardProduct company OA

Why is memcmp(&a, &b, sizeof a) == 0 an unreliable way to test whether two struct variables a and b have equal members?

  1. A. Padding bytes between or after members may hold different values even when every member is equal
  2. B. memcmp compares only the first member of a struct
  3. C. Struct addresses cannot be passed to memcmp
  4. D. memcmp stops comparing at the first zero byte

Correct answer: A. Padding bytes between or after members may hold different values even when every member is equal

memcmp compares raw bytes, including any padding the compiler inserted, whose contents are unspecified. Two structs with equal members can therefore compare unequal. (Floating-point members add more traps, such as 0.0 and −0.0.) memcmp does not stop at zero bytes; that is strcmp.

More C Programming questions

Exams that ask this topic