1

I am running a simple program to print the first element value of an array using a double pointer:

#include <stdio.h>

int main()
{
    int arr[3] = {1,2,3};
    int **ptr;
    *ptr = arr;
    printf("%d\n", **ptr);
    return 0;
}

But then I get a segmentation fault. What am I doing wrong?

4
  • 3
    When you dereference the pointer ptr in *ptr = arr it 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 plain arr is equal to &arr[0] and it has the type int *. Commented Jun 3, 2020 at 15:05
  • It is a test program. I was having this problem in a bigger one. Commented Jun 3, 2020 at 15:07
  • A "double pointer" is declared double *. A variable of type int ** is a pointer to pointer to int. Commented Jun 3, 2020 at 15:11
  • 1
    I'd say it still doesn't make any sense the way you use it like this (even when you make it work as in Vlad's answer). Commented Jun 3, 2020 at 15:12

1 Answer 1

2

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 
Sign up to request clarification or add additional context in comments.

3 Comments

"Or you could allocate the intermediate pointer dynamically like malloc( sizeof( int * ) );: allocating memory for a single pointer doesn't make much sense, even if the program is formally correct.
@Jabberwocky Consider implementations of linked lists where this is used very often.
In a linkes list you'd have something like malloc( sizeof( struct mystruct) ). I really can't think of a linked list implementation where you need to allocate dynamic memory for storing a single pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.