Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between pointer to pointer and pointer to array?

Given that the name of an array is actually a pointer to the first element of an array, the following code:

#include <stdio.h>

int main(void)
{
    int a[3] = {0, 1, 2};
    int *p;

    p = a;

    printf("%d\n", p[1]);

    return 0;
}

prints 1, as expected.

Now, given that I can create a pointer that points to a pointer, I wrote the following:

#include <stdio.h>                                                              

int main(void)                                                                  
{                                                                               
        int *p0;                                                                
        int **p1;                                                               
        int (*p2)[3];                                                           
        int a[3] = {0, 1, 2};                                                   

        p0 = a;                                                                 
        p1 = &a;                                                                
        p2 = &a;                                                                

        printf("p0[1] = %d\n(*p1)[1] = %d\n(*p2)[1] = %d\n",                    
                        p0[1], (*p1)[1], (*p2)[1]);                             

        return 0;                                                               
}

I expected it to compile and print

p0[1] = 1
(*p1)[1] = 1
(*p2)[1] = 1

But instead, it goes wrong at compile time, saying:

test.c: In function ‘main’:
test.c:11:5: warning: assignment from incompatible pointer type [enabled by default]

Why is that assignment wrong? If p1 is a pointer to a pointer to an int and a is a pointer to an int (because it's the name of an array of ints), why can't I assign &a to p1?

like image 617
jpmelos Avatar asked Jun 24 '11 21:06

jpmelos


People also ask

What is the difference between array to pointer and pointer to array?

A user creates a pointer for storing the address of any given array. A user creates an array of pointers that basically acts as an array of multiple pointer variables. It is alternatively known as an array pointer. These are alternatively known as pointer arrays.

What is difference between pointer and array?

An array is a collection of elements of similar data type whereas the pointer is a variable that stores the address of another variable. An array size decides the number of variables it can store whereas; a pointer variable can store the address of only one variable in it.

What is the difference between pointer and pointer to pointer?

Actually both pointers are the same: A memory position stored in memory. They only differ in what is stored at the memory position they point to.


2 Answers

Line 11 is

        p1 = &a;

where p1 has type int ** and a has type int[3], right?

Well; &a has type int(*)[3] and that type is not compatible with int** as the compiler told you

You may want to try

        p1 = &p0;

And read the c-faq, particularly section 6.

In short: arrays are not pointers, and pointers are not arrays.

like image 128
pmg Avatar answered Oct 18 '22 11:10

pmg


a is not a pointer to int, it decays to such in certain situations. If &a was of type int ** you couldn't very well use it to initialize p2, could you?

You need to do p1 = &p0; for the effect you want. "pointer to pointer" means "at this address, you will find a pointer". But if you look at the address &a, you find an array (obviously), so int ** is not the correct type.

like image 23
Chris Lutz Avatar answered Oct 18 '22 09:10

Chris Lutz