Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

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

0
Posted

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

0

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

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123