The pointer ptr is not initialized and has an indeterminate value.
int **ptr;
So dereferencing it results in undefined behavior.
*ptr = arr;
You need an intermediate pointer. For example
int *p;
int **ptr = &p;
*ptr = arr;
Or you could allocate the intermediate pointer dynamically like
int **ptr = malloc( sizeof( int * ) );
*ptr = arr;
//...
free( ptr );
Here are two demonstrative programs.
#include <stdio.h>
int main( void )
{
int arr[3] = {1,2,3};
int *p;
int **ptr = &p;
*ptr = arr;
printf("%d\n", **ptr);
return 0;
}
and
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int arr[3] = {1,2,3};
int **ptr = malloc( sizeof( int * ) );
*ptr = arr;
printf("%d\n", **ptr);
free( ptr );
return 0;
}
The both programs have the output
1
If you need a pointer to the whole array then its declaration can look as it is shown in the demonstrative program below.
#include <stdio.h>
int main( void )
{
enum { N = 3 };
int arr[N] = { 1, 2, 3 };
int ( *ptr )[N] = &arr;
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", ( *ptr )[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3
ptrin*ptr = arrit must already point somewhere. You're dereferencing an uninitialized pointer that doesn't point anywhere, leading to undefined behavior. Why do you need a "double pointer"? Arrays naturally decays to pointers to their first element, so using plainarris equal to&arr[0]and it has the typeint *.double *. A variable of typeint **is a pointer to pointer to int.