TonyK
TonyK

Reputation: 17124

Is this legal C/C++? `int* p = (int[]) {1,2,3} ;`

This answer of mine generated some comments claiming that the following construct is not legal C/C++:

void f (int* a) ;
f ((int[]){1,2,3,4,0}) ;

(see this ideone link for a full program). But we weren't able to resolve the issue. Can anybody shed any light on this? What do the various standards have to say?

Upvotes: 9

Views: 1653

Answers (3)

stativ
stativ

Reputation: 1490

Well, I think it is valid according to C++11. Section 5.2:

postfix-expression:
    ...
    typename-specifier ( expression-listopt )
    simple-type-specifier braced-init-list
    typename-specifier braced-init-list
    ...
expression-list:
    initializer-list

EDIT: After some more reading I came to conclusion it's actually invalid, because you cannot use postfix expression like that. There should be some primary expression.

Upvotes: -1

Adam Zalcman
Adam Zalcman

Reputation: 27233

The expression passed as argument to the function is an example of a compound literal. These are legal in C99, but not in C++98.

See for example section 6.4.4 "Constants" and 6.8 "Statements and blocks" in N897 "A draft rationale for the C99 standard." See also this section of GCC documentation.

Upvotes: 1

Mat
Mat

Reputation: 206831

It's valid C99 as far as I can tell - that's passing a compound literal.

The C99 standard has this as an example (§6.5.2.5/9):

EXAMPLE 1 The file scope definition

int *p = (int []){2, 4};

initializes p to point to the first element of an array of two ints, the first having the value two and the second, four. The expressions in this compound literal are required to be constant. The unnamed object has static storage duration.

Note that the (int []) thing is not a cast here.

This is not a valid C++ construct though, compound literals are not part of the C++ standard (C++11 included). Some compilers allow it as an extension. (GCC does, pass -Wall -pedantic to get a diagnostics about it. IBM xlC allows it as an extension too.)

Upvotes: 9

Related Questions