When I declare this function:
void vLFSR_ParseInput(unsigned char * pucDataArray,unsigned char unCount){}
and try to pass it this array
unsigned char g_ucUSCI_A0_RXBufferIndex = 0x00;
unsigned char g_ucaUSCI_A0_RXBuffer[RXBUFFERSIZE];
with this function call
vLFSR_Par开发者_JAVA百科seInput(&g_ucaUSCI_A0_RXBuffer,g_ucUSCI_A0_RXBufferIndex);
my compiler gives me this error
argument of type "unsigned char (*)[255]" is incompatible with parameter of type "unsigned char *"
What am I doing wrong? If it helps, I am programming using TI Code Composer Studio, and my platform is the MSP430x2xx family.
edit: fixed formatting
You should use:
vLFSR_ParseInput(g_ucaUSCI_A0_RXBuffer,g_ucUSCI_A0_RXBufferIndex);
^ no unary &
An array is implicitly convertable to a pointer to its initial element, so when you use g_ucaUSCI_A0_RXBuffer
in most contexts, it decays to a pointer to its initial element, as if you had written &g_ucaUSCI_A0_RXBuffer[0]
. The type of that is unsigned char*
.
When you apply the unary-&
to an array, it gives you the address of the array. This has the same pointer value, but its type is unsigned char (*)[RXBUFFERSIZE]
.
vLFSR_ParseInput(g_ucaUSCI_A0_RXBuffer,g_ucUSCI_A0_RXBufferIndex);
Arrays evaluate to pointer literals (meaning, roughly, a number whose type is a pointer to the type that the array is made of).
You were taking the address of a pointer literal ( similar to int *x = & 4;
) and trying to pass that in to a function that expected a pointer. You were trying to give it a pointer to a pointer.
And you were passing by reference, not by value. Passing arrays by value in C takes a lot of work, and is rarely done.
In an array, e.g. x[10], the "x" is treated as a pointer to a block of memory large enough to hold 10 elements (and the [] is a syntax for dereferencing this pointer to access individual elements within this block).
If you add an & in front of this, you are taking the address of the pointer, not the address of the array memory. That is, you have created a pointer-to-a-pointer, which is a (char **) rather than a (char *).
Just remove the & and it'll be fine.
精彩评论