array compound literal

L

Luca Forlizzi

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

Keith Thompson

Luca Forlizzi said:
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?
 
K

Keith Thompson

Luca Forlizzi said:
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).
 
I

Ian Collins

Luca said:
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?
 
L

Luca Forlizzi

Luca Forlizzi <[email protected]> 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.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top