Difference between pointer to pointer and pointer to array?

空扰寡人 提交于 2019-11-27 20:42:21

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!