array compound literal

Discussion in 'C Programming' started by Luca Forlizzi, Feb 11, 2010.

  1. Hello

    is it possible, in C99 standard, to use an array type compound
    literal as RHS of an assignment to a pointer variable?

    i.e.

    int *pi;
    pi = ( int [2] ){ 1, 3 };

    gcc seems to support such usage, but both Digital Mars Compiler and
    lcc do not.
    Luca Forlizzi, Feb 11, 2010
    #1
    1. Advertising

  2. Luca Forlizzi <> writes:
    > is it possible, in C99 standard, to use an array type compound
    > literal as RHS of an assignment to a pointer variable?
    >
    > i.e.
    >
    > int *pi;
    > pi = ( int [2] ){ 1, 3 };
    >
    > gcc seems to support such usage, but both Digital Mars Compiler and
    > lcc do not.


    Yes, I believe that's valid. The compound literal yields the value of
    an unnamed object with automatic storage duration of type int[2].
    Since it's of array type, it decays to int*.

    What error message(s) do the other compilers give you?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 11, 2010
    #2
    1. Advertising

  3. Luca Forlizzi <> writes:
    > is it possible, in C99 standard, to use an array type compound
    > literal as RHS of an assignment to a pointer variable?
    >
    > i.e.
    >
    > int *pi;
    > pi = ( int [2] ){ 1, 3 };
    >
    > gcc seems to support such usage, but both Digital Mars Compiler and
    > lcc do not.


    Do you mean lcc or lcc-win? lcc, as I recall, only supports C90;
    lcc-win has reasonably good C99 support (I don't know whether it's
    complete).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 11, 2010
    #3
  4. Luca Forlizzi

    Ian Collins Guest

    Luca Forlizzi wrote:
    > Hello
    >
    > is it possible, in C99 standard, to use an array type compound
    > literal as RHS of an assignment to a pointer variable?
    >
    > i.e.
    >
    > int *pi;
    > pi = ( int [2] ){ 1, 3 };
    >
    > gcc seems to support such usage, but both Digital Mars Compiler and
    > lcc do not.


    It should be legal. Do those compilers claim to be C99 compilers?

    --
    Ian Collins
    Ian Collins, Feb 11, 2010
    #4
  5. On 11 Feb, 21:03, Keith Thompson <> wrote:
    > Luca Forlizzi <> writes:


    > Do you mean lcc or lcc-win? lcc, as I recall, only supports C90;
    > lcc-win has reasonably good C99 support (I don't know whether it's
    > complete).
    >


    lcc-win. The compilers output is as follows.
    this is my test program:

    ---------------------------------
    #include <stdio.h>

    int b;

    int main(void) {

    int (*ap)[2];

    int *pi;
    int ai[4]={ 1,2,3,4};

    struct st {
    float a;
    char b;
    int c;
    } st_v;

    st_v = (struct st){ 2.3, 3, 300};
    pi = ai;
    pi = ( int [2] ){ 1, 3 };
    ap = &( (int [2]){ 4, 5});

    printf(" pi[0] = %d, pi[1] = %d\n", pi[0], pi[1]);
    printf(" (*ap)[0] = %d, (*ap)[1] = %d\n", (*ap)[0], (*ap)[1]);

    }
    ------------------------------------------------

    Compiled with "gcc -Wall -pedantic -std=c99" gives no diagnostics and
    the program
    prints the expected values.

    Digital Mars Compiler produces the following diagnostics:

    PS D:\temp\prove_c> dmc -A99 compound_literal.c
    st_v = (struct st){ 2.3, 3, 300};
    ^
    compound_literal.c(18) : Error: expression expected
    pi = ( int [2] ){ 1, 3 };
    ^
    compound_literal.c(20) : Error: expression expected
    ap = &( (int [2]){ 4, 5});
    ^
    compound_literal.c(21) : Error: expression expected
    --- errorlevel 1

    It seems dmc does not support compound literal, even for stucture
    types

    Lcc-win gives the following diagnostics:

    PS D:\temp\prove_c> lc -ansic compound_literal.c
    Error compound_literal.c: 20 the left hand side of the assignment
    can't be assigned to
    Error compound_literal.c: 21 the left hand side of the assignment
    can't be assigned to
    2 errors, 0 warnings
    1 error

    So the compound literal for the structure type is ok (as confirmed by
    other tests which successfully compile and run as expected), but the
    ones for array types are not.

    @Ian Collins
    Both lcc-win and DMC support many C99 features. dmc has compiler
    options for C89, C95, C99 and others. I don't know if they claim to be
    C99 fully conformant, probably not.
    Luca Forlizzi, Feb 12, 2010
    #5
    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. =?Utf-8?B?TWlrZUw=?=
    Replies:
    0
    Views:
    381
    =?Utf-8?B?TWlrZUw=?=
    Nov 19, 2004
  2. William Ahern

    lifetime of compound literal

    William Ahern, May 10, 2005, in forum: C Programming
    Replies:
    2
    Views:
    406
    Christian Kandeler
    May 11, 2005
  3. Lauri Alanko

    Compound literal without initializer?

    Lauri Alanko, Feb 16, 2011, in forum: C Programming
    Replies:
    3
    Views:
    337
    Lauri Alanko
    Feb 16, 2011
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    366
    Anonieko Ramos
    Sep 27, 2004
  5. Christopher Collins

    Size of a compound literal array

    Christopher Collins, Sep 12, 2013, in forum: C Programming
    Replies:
    1
    Views:
    217
    Eric Sosman
    Sep 12, 2013
Loading...

Share This Page