I've got array on the top of the code
int numberArray[] = {1, 2, 3, 4, 5};
And I would want to bring pointer from this array to another pointer in function
void movePointer(int* anotherPointer)
{
anotherPointer = numberArray;
}
And now I would use anotherPointer
in the rest of code. How I should to do this? I thougth about pointer from pointer, but I received nothing interesting.
void movePointer(int ** anotherPointer)
{
*anotherPointer = numberArray;
int a = (*anotherPointer)[1]; // value 2
}
Remember, that anotherPointer
is local variable - available only in body of this function. When you are passing variable as a pointer to the function, there is created a copy of this pointer inside this function. In this code:
void movePointer(int* anotherPointer)
{
anotherPointer = numberArray;
}
you are trying do modify the address stored in anotherPointer
. And you will, but only in the scope of this function. Outside that function the adress was not modified. The best solution is passing "pointer to pointer" as an argument like @i486 showed in his answer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.