Returning a structure without any temporary variable

X

Xavier Roche

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.
 
D

David RF

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.

It's a compound literal, valid in C99, gcc also supports compound
literals in C89 as an extension
 
J

jacob navia

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.
 
X

Xavier Roche

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
 
X

Xavier Roche

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.
 
J

James Kuyper

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.

That code involves two features introduced in C99: long long and a
compound literal. They're both syntax errors under C89.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top