Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Returning a structure without any temporary variable

Reply
Thread Tools

Returning a structure without any temporary variable

 
 
Xavier Roche
Guest
Posts: n/a
 
      06-01-2012
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.
 
Reply With Quote
 
 
 
 
David RF
Guest
Posts: n/a
 
      06-01-2012
On 1 jun, 10:14, Xavier Roche <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      06-01-2012
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.

 
Reply With Quote
 
Xavier Roche
Guest
Posts: n/a
 
      06-01-2012
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
 
Reply With Quote
 
Xavier Roche
Guest
Posts: n/a
 
      06-01-2012
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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-01-2012
On 06/01/2012 04:14 AM, Xavier Roche wrote:
> 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.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
Swap two integers without using temporary variable indrawati.yahya@gmail.com C++ 25 11-30-2007 09:41 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
returning copy of the temporary string object qazmlp C++ 3 03-07-2004 04:57 PM
iterator / returning reference to local temporary Alexander Stippler C++ 2 07-04-2003 04:40 PM



Advertisments