1. Home
  2. Computer Science MCQs
  3. C Programming
  4. A C99 struct is declared as struct Buf { size_t len; char…

Added 30 Nov -0001

A C99 struct is declared as struct Buf { size_t len; char data[]; }; on a 64-bit LP64 system. What does sizeof(struct Buf) evaluate to?

  1. The size of size_t plus any trailing padding, because the flexible array member holds no elements Correct answer
  2. The size of size_t plus one, because a flexible array member must always hold at least one element
  3. It is rejected at compile time, because an array member may not be declared with an empty subscript
  4. The size of a pointer, because data decays to a char pointer stored inside the struct

Correct answer

A. The size of size_t plus any trailing padding, because the flexible array member holds no elements

15 views

MCQSEXAM

Explanation

A flexible array member is an incomplete array type declared last in a C99 struct, and it contributes no elements to sizeof, so the struct is sized as if only the other members existed, plus trailing padding to the alignment. Storage is obtained with malloc(sizeof(struct Buf) + n) and the extra bytes are reached through data[0] to data[n-1]. The member must come last, the struct must have at least one other named member, and such a struct cannot be an array element; GNU's zero-length array data[0] is a non-standard cousin that looks the same but is not portable C99.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

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