0

I saw in internet that pointer to array and pointer to first element of array are the same things. But in CooCox next call an error:

//Get Arr
uint8_t TestDataArr[10];
//Func get pointer to arr
void InitData (TestPacks *Data)
{
//Some code
}
//This call error
InitData(&TestDataArr)
//But this is norm
InitData(&TestDataArr[0])

Why did it happen?

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Reawer
  • 3
  • 1

1 Answers1

0
InitData(&TestDataArr)

is not equal to

InitData(&TestDataArr[0])

cause TestDataArr[0] is equal to *TestDataArr and then InitData(&TestDataArr[0]) equal to InitData(&*TestDataArr) or InitData(TestDataArr). You can see, that

InitData(&TestDataArr)

is the address of TestDataArr and

InitData(TestDataArr)

just the array. That are also different types!

cmdLP
  • 1,658
  • 9
  • 19
  • 1
    They *are* the same address, only different types. – n. m. could be an AI Feb 27 '17 at 11:00
  • sorry, i mistaked. func init as 'void InitData (uint8_t*Data)'. Types are equal – Reawer Feb 27 '17 at 11:09
  • No, they do not point to the same memory. `&TestDataArr` is the pointer, which points to the `TestDataArr`-variable -- (type is `uint8_t**`/`uint8_t[10]*`). So it points to the pointer that points to the data. `TestDataArr` is pointing to the data directly. – cmdLP Feb 27 '17 at 11:10
  • 1
    For a given `uint8_t TestDataArr[10];` doing `&TestDataArr` results in a `uint8_t(*)[10]`. It does ***not*** result in a `uint8_t**`. – alk Feb 27 '17 at 11:13
  • Print them. What do you get? "it points to the pointer that points to the data" Why do you think such a pointer exists anywhere in memory? – n. m. could be an AI Feb 27 '17 at 11:32