David542
David542

Reputation: 110462

Throwaway buffer when necessary

Let's say I have the following function where I don't care about the tmp buffer required as an argument and only need the function's return value. For example:

char tmp[20];
printf("Using my function for case 1: %s", printstr(28, tmp));

Is the following an acceptable way to do the above?

printf("Using my function for case 1: %s",
   printstr(28, (char[20]){0})
);

Is that an acceptable approach? Or are there any downsides of using that approach?

Upvotes: 2

Views: 62

Answers (1)

Paul Hankin
Paul Hankin

Reputation: 58349

Section 6.5.2.5 paragraph 6 of the C standard says of compound literals:

The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.

So the two forms of the function call in your question are essentially the same.

Other than readability or subjective concerns, there's no reason to prefer one form to the other -- they have identical meanings as written.

Upvotes: 1

Related Questions