Interview Questions

How can I pass constant values to functions which accept structure arguments?

C Interview Questions and Answers


(Continued from previous question...)

How can I pass constant values to functions which accept structure arguments?

Q: How can I pass constant values to functions which accept structure arguments? How can I create nameless, immediate, constant structure values?

A: Traditional C had no way of generating anonymous structure values; you had to use a temporary structure variable or a little structure-building function;

C99 introduces ``compound literals'', one form of which provides for structure constants. For example, to pass a constant coordinate pair to a hypothetical plotpoint function which expects a struct point, you can call
plotpoint((struct point){1, 2});
Combined with ``designated initializers'' (another C99 feature), it is also possible to specify member values by name:
plotpoint((struct point){.x=1, .y=2});

(Continued on next question...)

Other Interview Questions