指针指针和数组指针的区别? [英] Difference between pointer to pointer and pointer to array?

查看:44
本文介绍了指针指针和数组指针的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于数组名实际上是指向数组首元素的指针,代码如下:

#include int main(void){int a[3] = {0, 1, 2};国际*p;p = a;printf("%d\n", p[1]);返回0;}

按预期打印 1.

现在,鉴于我可以创建一个指向指针的指针,我写了以下内容:

#include int main(void){int *p0;国际**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]);返回0;}

我希望它能编译和打印

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

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

test.c: 在函数‘main’中:test.c:11:5: 警告:从不兼容的指针类型赋值 [默认启用]

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

解决方案

第 11 行是

 p1 = &a;

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

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

你可能想试试

 p1 = &p0;

并阅读常见问题解答,尤其是第 6 节.

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

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?

解决方案

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.

这篇关于指针指针和数组指针的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆