So I'd like to ask the following question about some code my friend is asking me about
For the most part if I ever find myself working with Arrays I just allow myself to send the array's name as it decays to a pointer for example:
#include <stdio.h>
void foo(int *arr_p)
{
arr_p[0] = 111;
arr_p[1] = 222;
arr_p[2] = 333;
}
int main()
{
int arr[] = {1,2,3};
foo(arr);
printf("%d %d %d", arr[0], arr[1], arr[2]);
}
and as expected this will print 111, 222, 333
However I have a friend who asked me what would happen if for whatever reason I'd like to pass a pointer to a pointer of this array.
#include <stdio.h>
void foo(int **arr_p)
{
*arr_p[0] = 111;
*arr_p[1] = 222;
*arr_p[2] = 333;
}
int main()
{
int arr[] = {1,2,3};
foo(&arr);
printf("%d %d %d", arr[0], arr[1], arr[2]);
}
now this is the code he sent me... it has a segmentation fault and to perfectly honest I can't really figure out why. I'd imagine dereferencing the pointer to the array would leave me with a pointer to the array would it not?