I want to assign an array to a pointer variable. The way I am doing it is like this:

int array[] ={1,2,3};
int **p_array = &array;
(*p_array)[1] = 99;

But when I run the program, it fails. Could anyone help me with the issue?

Thanks in advance!

Recommended Answers

All 6 Replies

The reason yours won't compile is because two stars means a 2d array, but the array you declared is a 1d array.

int array[] ={1,2,3};
int *p_array = array;
p_array[1] = 99;

// or the last line could also be
*(p_array+1) = 99;

The reason yours won't compile is because two stars means a 2d array

I'd disagree there because it'll cause confusion when trying to assign a 2D array to a pointer to a pointer:

int a[2][3];
int **p = a; // Still won't work!

The problem is that the type of &array in the original snippet is int(*)[3]. This type is incompatible with int**.

You are applying "&" to an array variable. This works differently from pointers. It gives the memory address to the first element, so basically it can be considered int*. (cast to) Something like this will work as you'd expect it to:

#include <stdio.h>

int main (void)
{
    unsigned int i = 0;

    int array[] = {1,2,3};

    int *arrayAddr = (int*)&array;  // ArrayAddr is a pointer to the first element in "array".
    int **pArray   = &arrayAddr;    // pArray is a pointer to ArrayAddr.

    (*pArray)[1] = 99;              // Dereferencing "pArray" gives us "arrayAddr", [1] gives us *(arrayAddr + 1).

    // Show the new contents of "array".
    printf("Array: ");

    for (i = 0; i < sizeof(array) / sizeof(int); i++)
    {
        printf("%d ", array[i]);
    }

    return 0;
}

More about the differences between pointer types and array types (and this issue in particular) can be found here: https://blogs.oracle.com/ksplice/entry/the_ksplice_pointer_challenge

line 9: the typecast and & symbol are not necessary, just extra fluff that means nothing.

line 9: the typecast and & symbol are not necessary, just extra fluff that means nothing.

Actually, the typecast is necessary when using the address-of operator. Which brings up my next point.

int *arrayAddr = (int*)&array;

If you're forced to use a cast to make this work, it should tell you that something is wrong. While the address of &array may be the same as &array[0], the type is different. That's why you were forced to add a cast to make the line work. &array has a type of int(*)[3] and &array[0] has a type of int*; the two are not compatible and as such cannot be mixed and matched in an assignment.

If you take away the address-of operator, C gives you both the correct address and the correct type as syntactic sugar. Then you can omit the now redundant cast:

int *arrayAddr = array;

Or you can be explicit:

int *arrayAddr = &array[0];

Which was exactly my point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.