1

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

3
  • Note that your pBuff is pointing at memory on the stack which is no longer valid. Commented Nov 15, 2012 at 6:23
  • It's bad enough to have globals, but it's a really really really bad idea to have globals and parameters with the same names. Also, you need to get a basic grasp on the notion of types. pBuff has type void* but &pBuff has type void**, which should not be passed to a function that takes an argument of type void*. Commented Nov 15, 2012 at 6:41
  • @JesseGood: actually, it is not (in an even worse way). The local variable pBuff is set but assigning to a local variable has no effect on anything outside the function. The global variable pBuff is never actually modified. Commented Nov 15, 2012 at 9:48

4 Answers 4

3

Apart from the fact that you need to use:

((int*)pBuff)[i]

What you have in your code is Undefined Behavior.

pBuff = pPB;

pBuff points to a array which is local to the function and its lifetime does not exist beyond the function scope. So you have a pointer pointing to something that does not need to exist but may seemingly exist sometimes.

Sign up to request clarification or add additional context in comments.

1 Comment

actually, (confusinglly) the pBuff = pPB; line is fine, since that pBuff is a local variable.
2

You should probably dereference with * or [], not both at the same time :-)

If your intent is to get the integer at that i position of a void pointer which points to ints, use:

((int*)pBuff)[i]

The ((int*)pBuff) turns pBuff into a pointer to an integer and the [i] following that grabs the i'th integer at that location.

So your loop would be:

for (i = 0 ; i < 8; i++)
     printf ("\ndata : %d\n", ((int*)pBuff)[i]);

Another thing you should probably watch out for is returning pointer to stack-based variables. Those variables disappear when the function exits at which point dereferencing pointers to them is undefined behaviour.

Comments

1

pBuff[i] is illegal, since pBuff is a void*. It's a matter of operator precedence:

((int *)pBuff)[i]

You don't need to dereference pBuff again with the first * because [i] already does that.

2 Comments

there are other issues in his program that you are not addressing..specially the local scope problem
@Fake.It.Til.U.Make.It ah, missed that. Pointed out in the other answers now.
0

It should be

void * pBuff = NULL;

int
set_data(void *pBuff)
{
    int ptr = 10, i;
    int *phy_bn = (int*)malloc(sizeof(int)*8);
    //this is needed so that it is valid for pBuff to point phy_bn even if it is getting out of scope

    for(i=0; i<8; i++){
        phy_bn[i] = ptr;
    }

    pBuff = phy_bn;

    return 0;
}


int main()
{
    int i;

    set_data(&pBuff);

    for(i =0 ; i <8; i++){
         printf("\ndata : %d\n", ((int*)pBuff)[i]);
    }
    return 0;
}

4 Comments

No it shouldn't. set_data takes an argument of type void*, but &pBuff is of type void**. And setting pBuff at the end of set_data sets the parameter, which is never used, not the global. There are other problems with this code as well.
It says unhandled exception at ((int*)pBuff)[i] as an access voilation with 0xC0000005.
@JimBalter : what are those? It would be great if you correct them.
@Sam Those are that set_data pointlessly returns a value, malloc should not be cast, the argument to set_data should be void** pBuff, the final statement should be *pBuff = phy_bn, phy_bn can't be on the stack ... maybe there are more as well. A basic question: why use void* when int* is intended?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.