c - Expression for struct creation -


Is it possible to create strokes "inline" in C?

  typedef struct {int x; Int y; } Point; Point f (int x) {point retval = {.x = x, .y = x * x}; Return return; } Point G (int x) {return {.x = x, .y = x * x}; }  

f is valid, g no. The same applies to function calls:

  float distance (point one, point B) {return 0.0; } Int main () {distance ({0, 0}, {1, 1}}}  

Is it possible to create these strokes without using the temporary variable Compiler I will be optimized, but readability matters a lot)?

With a C99 compiler, you can do this.

  dot g {return (point) {.x = x, .y = x * x}; }  

Your call will be for distance :

  distance ((point) {0, 0}, (point) {1, 1})  

They are called Compound Literals, for example, see, for some information.


Comments