Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Returning a structure without any temporary variable

17 views
Skip to first unread message

Xavier Roche

unread,
Jun 1, 2012, 4:14:45 AM6/1/12
to
Hi folks,

Is the following code standard ? (which one ?)

typedef struct my_t {
double d;
unsigned long long l;
} my_t;

void my_t getOne() {
return (my_t) { 1.0 };
}

My feeling is that the code might be correct in C99, but not in C89.

David RF

unread,
Jun 1, 2012, 4:45:28 AM6/1/12
to
It's a compound literal, valid in C99, gcc also supports compound
literals in C89 as an extension

jacob navia

unread,
Jun 1, 2012, 4:50:07 AM6/1/12
to
Le 01/06/12 10:14, Xavier Roche a écrit :
> void my_t getOne() {
> return (my_t) { 1.0 };
> }

The specification of the function is wrong:

void my_t getOne() is a syntax error.

It's either void or returning a my_t.

This is always incorrect, in ANY version of C you may use.

Xavier Roche

unread,
Jun 1, 2012, 4:54:15 AM6/1/12
to
On 06/01/2012 10:50 AM, jacob navia wrote:
> The specification of the function is wrong:
> void my_t getOne() is a syntax error.

Yes, unfortunate typo (the real one was my_t) ; the question behind
concerned the "compound literal", which was indeed C99

Xavier Roche

unread,
Jun 1, 2012, 4:57:10 AM6/1/12
to
On 06/01/2012 10:45 AM, David RF wrote:
> It's a compound literal, valid in C99, gcc also supports compound
> literals in C89 as an extension

Thanks -- fortunately, compilers appears to be smart enough not to
create the intermediate structure on the stack when unneeded.

James Kuyper

unread,
Jun 1, 2012, 6:08:46 AM6/1/12
to
That code involves two features introduced in C99: long long and a
compound literal. They're both syntax errors under C89.
--
James Kuyper
0 new messages