指针指向指针和指针指向数组的区别?

时间:2022-09-06 14:39:43

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.

打印1,如预期。

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?

为什么这个任务是错误的?如果p1是指向int的指针,a是指向int的指针(因为它是ints数组的名称),为什么我不能赋值给p1 & &a呢?

3 个解决方案

#1


16  

Line 11 is

第11行是

        p1 = &a;

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

p1的类型是int ** a的类型是int[3],对吧?

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

好,&a有类型int(*)[3],并且该类型与编译器告诉您的int**不兼容

You may want to try

你可能想试试

        p1 = &p0;

And read the c-faq, particularly section 6.

阅读c-faq,特别是第6部分。

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

简而言之:数组不是指针,指针也不是数组。

#2


6  

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?

a不是指向int的指针,它在某些情况下会衰减成这样。如果&a是int **你不能很好地用它初始化p2,你能吗?

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.

你需要做p1 = &p0;为了达到你想要的效果。“指针指向指针”的意思是“在这个地址,你会找到一个指针”。但是如果你看一下地址&,你会发现一个数组(显然),所以int **不是正确的类型。

#3


0  

For many operations, a implies &a and both return the same thing: The address of the first item in the array.

对于许多操作,a表示和&并返回相同的内容:数组中第一个项的地址。

You cannot get the address of the pointer because the variable does not store the pointer. a is not a pointer, even though it behaves like one in some cases.

您无法获得指针的地址,因为变量不存储指针。a不是指针,即使它在某些情况下表现得像指针。

#1


16  

Line 11 is

第11行是

        p1 = &a;

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

p1的类型是int ** a的类型是int[3],对吧?

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

好,&a有类型int(*)[3],并且该类型与编译器告诉您的int**不兼容

You may want to try

你可能想试试

        p1 = &p0;

And read the c-faq, particularly section 6.

阅读c-faq,特别是第6部分。

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

简而言之:数组不是指针,指针也不是数组。

#2


6  

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?

a不是指向int的指针,它在某些情况下会衰减成这样。如果&a是int **你不能很好地用它初始化p2,你能吗?

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.

你需要做p1 = &p0;为了达到你想要的效果。“指针指向指针”的意思是“在这个地址,你会找到一个指针”。但是如果你看一下地址&,你会发现一个数组(显然),所以int **不是正确的类型。

#3


0  

For many operations, a implies &a and both return the same thing: The address of the first item in the array.

对于许多操作,a表示和&并返回相同的内容:数组中第一个项的地址。

You cannot get the address of the pointer because the variable does not store the pointer. a is not a pointer, even though it behaves like one in some cases.

您无法获得指针的地址,因为变量不存储指针。a不是指针,即使它在某些情况下表现得像指针。