1

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
DiBosco
  • 838
  • 8
  • 21

2 Answers2

9
  • &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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • is `dataArray` not also a correct representation of pointer to array? – ryyker Jun 17 '15 at 13:12
  • @ryyker Updated my answer. Please have a look :-) – Sourav Ghosh Jun 17 '15 at 13:18
  • 1
    _None of them is a pointer-to-pointer here. They are just pointer with different types_ is the key phrase that clearly summarizes the bullet points you provide in your response. Good clarification. – ryyker Jun 17 '15 at 13:44
  • I'm well aware that &dataArray[0] and dataArray are the same - and both pointers to a char, I as much as said it in the original post. :) I'm still not sure of whether the compiler should have given a warning or error because he was passing, what I now believe to be a pointer to an array, rather than a pointer to a char. Also, in this particular instance if you incremented the pointer to the array as this is, the address should increment by ten because it's an array of ten chars? – DiBosco Jun 17 '15 at 14:31
  • 2
    @DiBosco No, that is wrong. ` &dataArray[0]` and `dataArray` are not the same. The former is the pointer to the first element of the array `dataArray`, whereas the second is the array itself. **An array is NOT a pointer!!!** – The Paramagnetic Croissant Jun 17 '15 at 16:55
  • Really? I've often seen the two as interchangeable and that claim goes directly against what others have said here! – DiBosco Jun 17 '15 at 17:20
  • 1
    @DiBosco Yeah, really. Go read the standard. "An array is often implicitly converted to a pointer" is not the same as "an array is a pointer". Also, try printing the result of `sizeof(dataArray)` and `sizeof(&dataArray[0])`. And read [this](https://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers). – The Paramagnetic Croissant Jun 18 '15 at 14:03
  • "Apart from that, the value will be the same" should rather read: "they point to the same address... `&dataArray[0]` and `&dataArray` need not be compatible. – Antti Haapala -- Слава Україні Feb 27 '17 at 11:11
  • @AnttiHaapala Thank you sir for the advice, makes perfect sense, updated accordingly. – Sourav Ghosh Feb 27 '17 at 11:20
0

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)

Kunal Saini
  • 138
  • 10