1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Two struct objects hold identical member values but were…

Added 30 Nov -0001

Two struct objects hold identical member values but were filled separately. Why can memcmp on them be unreliable?

  1. Assignment performs a shallow copy, so the two structs end up sharing member storage
  2. Padding bytes may hold indeterminate values, so a byte comparison can report differences Correct answer
  3. The member-wise copy converts floating values, so bit patterns can differ in the last unit
  4. memcmp inspects only the first member of the struct, ignoring all the trailing members

Correct answer

B. Padding bytes may hold indeterminate values, so a byte comparison can report differences

13 views

MCQSEXAM

Explanation

Structure padding is not part of any member's value, and the standard leaves those bytes unspecified after assignment, so two objects with equal members may differ bytewise. Comparing structs with memcmp is safe only after zeroing them or when the type has no padding. Comparing member by member also avoids the trap of -0.0 differing from 0.0 for floating members.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? 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