Can the pointer arithmetic be done on the void pointer? If not, what to do when void pointer is having the
address of some integer or other data type and we want to increment it?
Can the pointer arithmetic be done on the void pointer? If not, what to do when void pointer is having the
address of some integer or other data type and we want to increment it?
No.Originally Posted by csvraju
You need to cast the void pointer to a pointer to the appropriate data type, increment the result, then assign it back.Originally Posted by csvraju
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
is the declaration correct for type casting a void pointer
*(char *)vptr;
(*(char *)vptr)++; will the void pointer increment
This bit of code
will incrementCode:(*(char *)vptr)++;
which, if you read it out, is vptr, casted to a pointer-to-char, and then dereferenced to get a char. So, no, you will not increment the pointer.Code:(*(char *)vptr))
using void pointers we can increment the value of char and integers .......
but i am not able to increment the value of a float variableCode:#include <stdio.h> int main() { int i=10; char ch='B'; float f = 9.8; int *iptr; char *cptr; float *fptr; void *vptr; iptr = &i; cptr = &ch; vptr = cptr; (*(char *)vptr)++; printf("Incremented value of ch is %c\n",ch); vptr = iptr; (*(int *)vptr)++; printf("incremented value of i is %d\n",i); vptr = fptr; (*(float *)vptr)++; printf("incremented value of f is %f\n",f); getch(); return 0; }
your fptr is not initialized
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
ok i got it.......
the problem is solved