Even after casting a void pointer, I am getting compilation error while dereferencing it. Could anyone please let me know the reason of this.
int lVNum = 2;
void *lVptr;
lVptr = (int*)&lVNum;
printf("\nlVptr[60 ] is %d \n",lVptr[1]);
You cannot. Dereferencing a void pointer requires an explicit cast beforehand. You can ofcourse cast it to any particular type and then dereference it without knowing its original type, but why you would want to do that is beyond me.
Explanation: Because the void pointer is used to cast the variables only, So pointer arithmetic can't be done in a void pointer.
It is not safe to delete a void pointer in C/C++ because delete needs to call the destructor of whatever object it's destroying, and it is impossible to do that if it doesn't know the type.
The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer.
It's not allowed to dereference void pointer. You need to cast it to another pointer type: Similarly, you can't assign anything to *a. Right code is: Show activity on this post. Because you are dereferencing void pointers:
C dereference pointer As we already know that "what is a pointer", a pointer is a variable that stores the address of another variable. The dereference operator is also known as an indirection operator, which is represented by (*). When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer.
(void*)0 is a null pointer constant, whose value is a null pointer of type void* , so by the semantics of parenthesized expressions ( (void*)0) also has a value that is a null pointer of type void* . Both (void*)0 and ( (void*)0) are address constants. What is dereferencing a pointer in C?
If you want free form access to memory, cast the pointer to a uint8_t *. This will cast the void pointer to a pointer to an int and then dereference it correctly. If you want to treat it as an array (of one), you could do a slightly ugly ( (int *)lVptr) [0]. Using [1] is out of bounds, and therefore not a good idea (as for lVptr [60] ...)
It doesn't make sense to dereference a void pointer. How will the compiler interpret the memory that the pointer is pointing to? You need to cast the pointer to a proper type first:
int x = *(int*)lVptr;
printf("\nlVptr[60 ] is %d \n", *(int*)lVptr);
This will cast the void pointer to a pointer to an int
and then dereference it correctly.
If you want to treat it as an array (of one), you could do a slightly ugly ((int *)lVptr)[0]
. Using [1]
is out of bounds, and therefore not a good idea (as for lVptr[60]
...)
It's still a void*
because that's what you declared it as. Any pointer may be implicitly converted to a void*
, so that cast does nothing and you are left with a pointer to void
just as you began with.
You'll need to declare it as an int*
.
void *some_ptr = /* whatever */;
int *p = (int*)some_ptr;
// now you have a pointer to int cast from a pointer to void
Note that the cast to an int*
is also unnecessary, for the same reason you don't have to (and should not) cast the return value of malloc
in C.
void*
's can be implicitly converted to and from any other pointer type. I added the cast here only for clarity, in your code you would simply write;
int *p = some_void_ptr;
Also, this:
lVptr[1]
Is wrong. You have a pointer to a single int
, not two. That dereference causes undefined behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With