How can I pass constant values to functions which accept structure arguments? How can I create nameless, immediate, constant structure values?
Traditional C had no way of generating anonymous structure values; you had to use a temporary structure variable or a little structure-building function; see question 14.11 for an example. 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}); See also question 4.10. References: C9X Sec. 6.3.2.5, Sec. 6.5.8 comp.lang.c FAQ list ยท Question 2.11 Q: How can I read/write structures from/to data files? A: It is relatively straightforward to write a structure out using fwrite: fwrite(&somestruct, sizeof somestruct, 1, fp); and a corresponding fread invocation can read it back in. What happens here is that fwrite receives a pointer to the struct