Void pointer in c++


In c++
int main() {
int i = 99;
void* vp = &i;
*vp = 3; }
Why there is compile time error at line 4.

Comments

  1. *vp = 3;

    vp is a void pointer. pointing to void. not an integer pointer. so cant assign an integer at the location pointed by void pointer.?

    ReplyDelete
  2. @Ramesh yes you can assign value at it.But Void pointer can be used to point at any kind of variable but on dereferencing you must cast it.
    eg : In above case *((int*)vp) = 3 will work.try it..

    ReplyDelete
  3. @priyaranjan u said dat u can assign value at it but again u r saying dat u hav to dereference it. i think its a conflict dude. when u r typecasting something it no longer behaves like a void pointer.

    ReplyDelete
  4. @above please elaborate what you want to say...

    ReplyDelete

Post a Comment

Popular posts from this blog