Is this a pointer to a pointer of the start of an array?

六眼飞鱼酱① 提交于 2019-12-17 03:44:49

问题


I've just been helping someone out with some code. He had this:

char dataArray[10];

Then wanted to get a pointer to the start of the array. Rather than use:

&dataArray[0]

or just

dataArray

He used

&dataArray

Did he end up with a pointer to a pointer there? I'm not sure what &dataArray would give him.


回答1:


  • &dataArray[0] is of type char *. That is a pointer to char.
  • dataArray is of type char[10]
  • &dataArray will be of type char (*)[10]. That is a pointer-to-array.

Apart from that, the value will be same, i.e., they point to the same address but their types need not be compatible.

None of them is a pointer-to-pointer here. They are just pointer with different types.

Note: Because the array decaying property, char [100] will decay to a char *, for example, when passed as an agument of a function.




回答2:


dataArray and &dataArray[0] points to the address of the first index of the array so they are the same thing but &dataArray will point to the address of the whole array or we can say it will give the address where the whole 10 index array is located(as a whole)



来源:https://stackoverflow.com/questions/30892206/is-this-a-pointer-to-a-pointer-of-the-start-of-an-array

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