0

Is there a notation to initialize a pointer to an array in a single-line, such as with an object literal? Here is what I am currently doing:

int arr[] = {1,2,3};
const int* const x = (const int*) &arr;

And I was wondering if there's a way to do the equivalent of something like:

const int* const x = {1,2,3}; 
7
  • What do you mean? int arr[] = {1,2,3}; initializes an array in a single line. x is not an array. Do you wonder how to initialize a pointer to point at the first item of an array on a single line? The main problem with the code you posted is wrong use of type qualifiers, not related to arrays. It could probably just be const int* x = arr; to make sense. Commented Feb 4, 2022 at 11:58
  • @Lundin const int* x would allow changing the pointer though: godbolt.org/z/43s7a3Tc6 Commented Feb 4, 2022 at 16:42
  • Is there a reason why you take the address and cast arr? Just writing const int* const x = arr; should give the same result. Commented Feb 4, 2022 at 21:03
  • 1
    An array type expression is implicitly converted to a pointer to the first element of the array (en.cppreference.com/w/c/language/conversion). Commented Feb 4, 2022 at 21:20
  • 1
    Yes, a "pointer to const" only says that we cannot change the element through that pointer, it does not mean that the element itself is const, so we can make a "pointer to const" point to a constant or non-constant element. What we cannot do is to make a "pointer to non-const" point to a constant element. E.g. const int arr[] = {1,2,3}; int * const x = arr; should at least give a compiler warning. Commented Feb 4, 2022 at 22:36

1 Answer 1

4

Is there a notation to initialize an array in a single-line, such as with an object literal?

Yes, C has had compound literals since C99, and these can be expressed for array types (and for scalar types too, for that matter). The syntax would be similar to what you suggested:

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

or

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

or

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

It is important to understand that the parenthesized type name in each of those is not a typecast operator, but instead an integral part of the syntax for a compound literal: a parenthesized type name followed by a curly-braced initializer list.

It is also important to understand that the object represented by the compound literal has static storage duration if it appears at file scope but automatic storage duration if it appears at block scope, just like a named object.

A compound literal is an lvalue.

In this particular case, in addition to the compound literal itself, the assignment to a pointer makes use of the usual automatic conversion of a value of array type to a pointer to the first array element.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for this. Maybe a stupid question, but why does doing something like (int[]) work but (int*) would result in an error for the compound literal syntax?
@David542, because pointers and arrays are completely different kinds of objects. If you use (int[]) then you are forming a compound literal of an array, whose length is determined from the initializer list. If you use (int*) then you are forming a compound literal of a (single) pointer. In the latter case, (i) there are too many elements in the initializer list, and (ii) the initializer elements are all integers, and integer-to-pointer conversions require a cast (or exercise of a language extension).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.