Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass "pointer to array" in function

Tags:

c

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..?

like image 452
Jeegar Patel Avatar asked Aug 01 '11 09:08

Jeegar Patel


4 Answers

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.

like image 181
Karl Knechtel Avatar answered Oct 21 '22 17:10

Karl Knechtel


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);
}
like image 23
Sean Avatar answered Oct 21 '22 17:10

Sean


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

like image 31
Armen Tsirunyan Avatar answered Oct 21 '22 17:10

Armen Tsirunyan


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.

like image 40
NPE Avatar answered Oct 21 '22 17:10

NPE