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

发布于 2024-11-17 02:48:43 字数 1791 浏览 7 评论 0原文

鉴于数组的名称实际上是指向数组第一个元素的指针,以下代码:

#include <stdio.h>

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

    p = a;

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

    return 0;
}

按预期打印 1

现在,考虑到我可以创建一个指向指针的指针,我编写了以下内容:

#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;                                                               
}

我期望它能够编译并打印,

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

但相反,它在编译时出错,说:

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

为什么分配错误?如果 p1 是指向 int 的指针,而 a 是指向 int 的指针(因为它是int 数组的名称),为什么我不能将 &a 分配给 p1

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一口甜 2024-11-24 02:48:43

第 11 行是

        p1 = &a;

p1 的类型为 int **a 的类型为 int[3],对吗?

出色地; &a 具有类型 int(*)[3] 并且该类型与 int** 不兼容,正如编译器告诉您的那样

您可能想要尝试

        p1 = &p0;

并阅读c-faq,特别是第6节。

简而言之:数组不是指针< /strong> 和指针不是数组

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.

装纯掩盖桑 2024-11-24 02:48:43

a 不是指向 int 的指针,它在某些情况下会衰减。如果 &a 的类型为 int **,您就不能很好地使用它来初始化 p2,可以吗?

您需要执行 p1 = &p0; 以获得您想要的效果。 “指向指针的指针”的意思是“在这个地址,你会找到一个指针”。但是,如果您查看地址 &a,您会发现一个数组(显然),因此 int ** 不是正确的类型。

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.

椵侞 2024-11-24 02:48:43

对于许多操作,a 意味着 &a 并且都返回相同的内容:数组中第一项的地址。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文