Hi, I observed a strange behaviour (for me only) of void pointer. I wrote a code to access an integer through void pointer as below:
void *ptr; int a[2] = {1,2};
ptr = (int *)&a; ptr++; // This is giving error: Unknown Size
Why the above increment statement is giving error as i already cast the pointer to int type ?
Can anybody help me in understanding this ?
Karthik K.
((int *)ptr)++;
Jon
whats the correct way?
Depends on what you are trying to do. If you are really only going to use this pointer to point to int, then make it a pointer to int. If not, explain what you are trying to do.
- mike
then whats the correct way?
Setting a void pointer to the address of an object does not change the pointer's type; it's still a void pointer.
For example, if you assigned the address of a function to a void pointer and then tried to increment the pointer, what result would you expect?
i already cast the pointer to int type ?
No. You didn't. You cast the expression &a to an int pointer type.
View all questions in Keil forum