Jeegar Patel
Jeegar Patel

Reputation: 27210

how to pass "pointer to array" in function

This is my function

int mkvCopyWord(uint8_t *pBuffer, UINT8 temp):

main() 
{
uint8_t a[10];
mkvCopyWord(&a,10);
}

its says warning : note: expected ‘uint8_t *’ but argument is of type ‘uint8_t (*)[10]’

how to remove this warning..?

Upvotes: 6

Views: 22025

Answers (4)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

int main(void) //main() without return type isn't a good practice  
{
   uint8_t a[10];
   mkvCopyWord(&a,10);
   return 0; 
}

Here you pass a parameter of type "pointer to array of 10 uint8_t". Your function, however, is declared to take pointer to int. In order to make this syntax work, you'd have to change the declaration of your function to take a pointer to an array of 10 uint8_t's

void mkvCopyWord(uint8_t (*parr)[10], int n)

This is all about syntax and type system. As for your actual problem, you need to pass a pointer to the first element of your array.

mkvCopyWord(&a[0],10);

However, an array is implicitly converted to that anyway, so you don't need to bother. You use the following:

mkvCopyWord(a,10);  

HTH

Upvotes: 5

Karl Knechtel
Karl Knechtel

Reputation: 61509

Your syntax for passing a pointer-to-array is fine. But you are trying to pass it to something that doesn't want a pointer-to-array. It just wants a pointer to the beginning element. The simplest way to get that is to allow the array name to decay to a pointer, thus mkvCopyWord(a, 10). The function will assume that the pointer you give it is pointing to some sequence of uint8_ts somewhere - that is why you have to pass the array size as another parameter, because it does not know about the array, it only has some address in memory where the array is.

Upvotes: 10

Sean
Sean

Reputation: 62472

You don't need to take the address of the array as it implicitly converts to a pointer:

int mkvCopyWord(uint8_t *pBuffer, UINT8 temp):

main() 
{
  uint8_t a[10];
  mkvCopyWord(a,10);
}

Upvotes: 7

NPE
NPE

Reputation: 500327

Remove the ampersand in mkvCopyWord(&a,10):

int mkvCopyWord(uint8_t *pBuffer, UINT8 temp):

main() 
{
  uint8_t a[10];
  mkvCopyWord(a,10); /* <<< removed the ampersand */
}

There is quite a lot of information on the equivalence of pointers and arrays in the C FAQ.

Upvotes: 3

Related Questions