Reputation: 707
I have a variable that needs to hold a pointer to an array. Normally this is done by first creating a variable with the array, then saving the pointer to that variable. However, I need to do this for maybe 10 variables, and I don't want to have to create an extra array for each of them. What I first tried was
int *numList = {1, 2, 3};
But that tries to create an array of pointers to integers. I think the following has the correct type for the list, but I don't know how to cast the array to a pointer.
int (*numList)[3] = {1, 2, 3};
Upvotes: 10
Views: 4401
Reputation: 804
The standard says:
Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue.
You can assign the array into a type of int[]
, since "casting the array to a pointer" is not required by the very reason of arrays decaying into pointers by itself so to speak.
In short, int[]
is treated like an int*
from a practical point of view. Arrays are effectively pointers to their first element:
int array[] = { 1, 2, 3 };
Now you can pass this object to a function with the prototype void func(int *array);
without casting it to int *
. That is, you can call it as func(array)
.
Some relevant fact about this is array == &array
. Consequently, you never need to explicitly cast an array into a pointer as that would be redundant.
Check What is array decaying? for more information.
Upvotes: 1
Reputation: 754410
In C99 and C11, you can use a compound literal to achieve what is requested in the question title, namely initialize a pointer so it points to an array literal without any extra variables:
int *numList = (int []){ 1, 2, 3 };
The array that numList
points to has the same scope and lifetime as the pointer itself — it lasts for the block it is defined in if it is inside a function, or for the duration of the program if it is outside any function.
If you were so misguided as to create static int *numList = (int []){ 1, 2, 3};
inside a function, you'd be setting yourself up for a world of hurt — don't do it. (At file scope, outside any function, it doesn't present any problem.) The pointer would be initialized no later than when the function is first called, but there's no guarantee that what it points to has static
duration and subsequent calls could be pointing at garbage.
Upvotes: 12
Reputation: 1
int (*numList)[3];
FYG, this is a pointer to an array of 3 integers (square brackets have higher precedence than *); you cannot initialize it this way.
Upvotes: -1
Reputation: 398
The array name itself is a pointer to the first element of the array.
ie, in your example,
int numList[3]={1,2,3};
numList points to 1.
if you want to pass this array's pointer to a function like
void sort(int *array)
{
//do something
}
you can call this function with your array's name as argument.
sort(numList);
Upvotes: -1