arrays and pointers - C++ Forum
Confused about pointer pointers and array pointers
c - Direct assignment of int array to pointer - Stack Overflow
c - How can I assign an array to pointer? - Stack Overflow
Videos
So if make an array like this:
int v[4][2];
I can’t assign v to:
int **pptr;
Instead I can assign it to:
int (*aptr)[2];
But I can pptr into a 2d array with malloc:
pptr = malloc( 4 * sizeof(int *));
for (int i = 0; i < 4; i++)
pptr[i] = malloc(2 * sizeof(int));Now I can assign a number to pptr[2][1] similar to v, but I can also assign pptr into another pointer pointer without any problems unlike v. This is so confusing, can someone explain why this happens?
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.
{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.)