Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array compound literal

Reply
Thread Tools

array compound literal

 
 
Luca Forlizzi
Guest
Posts: n/a
 
      02-11-2010
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-11-2010
Luca Forlizzi <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-11-2010
Luca Forlizzi <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-11-2010
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
 
Reply With Quote
 
Luca Forlizzi
Guest
Posts: n/a
 
      02-12-2010
On 11 Feb, 21:03, Keith Thompson <(E-Mail Removed)> wrote:
> Luca Forlizzi <(E-Mail Removed)> 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(1 : 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.

 
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
Compound literal without initializer? Lauri Alanko C Programming 3 02-16-2011 11:07 AM
lifetime of compound literal William Ahern C Programming 2 05-11-2005 07:14 AM
RE: Compound Control event not firing, but only when it's in a library =?Utf-8?B?TWlrZUw=?= ASP .Net 0 11-19-2004 04:45 AM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
CMR/CMP and Compound Primary Key Damir Mikoc Java 1 07-04-2003 03:27 AM



Advertisments