Home > Software design >  Direct assignment of int array to pointer
Direct assignment of int array to pointer

Time:06-10

Was trying to brush up my C concepts and now it seems all jumbled up :( in pointers, of course

This way of assigning arr to ptr works since we say arr[] will decay to *arr

int arr[] = {1, 2, 3};
int *ptr = arr;    // Access arr with ptr

But directly assigning the array to *ptr doesn't work

int *ptr = {1, 2, 3};
printf("%d\n", ptr[0]); // Segmentation fault

My understanding is that int arr[] = {} has a special meaning where a contiguous chunk of stack space is allocated and is directly referred to by the name arr

Trying to do the same thing with int *ptr = {} is just confusing the compiler ??

CodePudding user response:

The variable ptr is a scalar object. You may initialize a scalar object with a braced list that contains only one expression.

From the C Standard (6.7.9 Initialization)

11 The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.

So instead of:

int *ptr = {1, 2, 3};

you may write for example:

int *ptr = { 3 };

that is equivalent to:

int *ptr = 3;

and does not make a sense and the compiler can issue a message that an integer is converted to a pointer without casting.

Instead of the braced list you could initialize the pointer with a compound literal like:

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

In this declaration there is created an unnamed array of the type int[3] and the address of the first element of the array is assigned to the pointer.

CodePudding user response:

{1, 2, 3} does not mean “an array”. In a declaration, it is a list of initial values for some object with multiple parts.

In int arr[] = {1, 2, 3};, {1, 2, 3} is a list of three values to use to initialize the array arr.

In int *ptr = {1, 2, 3};, {1, 2, 3} would be a list of three values to use to initialize the pointer ptr. But ptr does not have multiple parts. All it has is one value, a memory address. So {1, 2, 3} would provide 1 to initialize the address, which is a problem because 1 is an int, not an address, so the compiler should issue a diagnostic message for that. And there is nothing for 2 or 3 to initialize, so the compiler should issue a diagnostic message for that.

You can use a compound literal to create an unnamed array directly in source code. A compound literal has the form (type) { initial values }. So you can create an array of int with (int []) {1, 2, 3}.

You can declare a pointer and initialize it to point to an array by using a compound literal as the initial value:

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

(Note that the array is automatically converted to a pointer to its first element, so ptr is initialized to that address.)

  • Related