I have a pointer to array of fixed size integer elements. After populating that array, I assigned it to void *pBuff. Later on, I need to access array elements through void pointer which I failed in doing so.
Here is the code using C:
void * pBuff = NULL;
int
set_data(void *pBuff)
{
int ptr = 10, i;
int phy_bn[8] = {0};
int (*pPB)[8];
for(i=0; i<8; i++){
phy_bn[i] = ptr;
}
pPB = &phy_bn;
pBuff = pPB;
return 0;
}
int main()
{
int i;
set_data(&pBuff);
for(i =0 ; i <8; i++){
printf("\ndata : %d\n", *(int *)pBuff[i]);
}
return 0;
}
It prompts an error cast of 'void' term to non-'void' against *(int *)pBuff[i].
Any help will be really appreciated.
Thanks,
-Sam
pBuffis pointing at memory on the stack which is no longer valid.pBuffhas typevoid*but&pBuffhas typevoid**, which should not be passed to a function that takes an argument of typevoid*.pBuffis set but assigning to a local variable has no effect on anything outside the function. The global variablepBuffis never actually modified.