Returning a structure without any temporary variable

Discussion in 'C Programming' started by Xavier Roche, Jun 1, 2012.

  1. Xavier Roche

    Xavier Roche Guest

    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.
    Xavier Roche, Jun 1, 2012
    #1
    1. Advertising

  2. Xavier Roche

    David RF Guest

    On 1 jun, 10:14, 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.


    It's a compound literal, valid in C99, gcc also supports compound
    literals in C89 as an extension
    David RF, Jun 1, 2012
    #2
    1. Advertising

  3. Xavier Roche

    jacob navia Guest

    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.
    jacob navia, Jun 1, 2012
    #3
  4. Xavier Roche

    Xavier Roche Guest

    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, Jun 1, 2012
    #4
  5. Xavier Roche

    Xavier Roche Guest

    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.
    Xavier Roche, Jun 1, 2012
    #5
  6. Xavier Roche

    James Kuyper Guest

    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
    James Kuyper, Jun 1, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Stippler
    Replies:
    2
    Views:
    624
    Alexander Stippler
    Jul 4, 2003
  2. qazmlp
    Replies:
    3
    Views:
    386
    Claudio Puviani
    Mar 7, 2004
  3. marco_segurini

    Returning a reference to a temporary

    marco_segurini, Jan 10, 2005, in forum: C++
    Replies:
    1
    Views:
    352
    Dietmar Kuehl
    Jan 10, 2005
  4. Replies:
    25
    Views:
    1,832
    James Kanze
    Nov 30, 2007
  5. Replies:
    7
    Views:
    3,202
    James Kanze
    Feb 12, 2008
Loading...

Share This Page