Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereference void pointer

Tags:

c

pointers

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]);
like image 669
Saurabh Ghorpade Avatar asked Mar 18 '13 01:03

Saurabh Ghorpade


People also ask

Can you dereference a void pointer without knowing its type?

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.

What can't you do on a void pointer?

Explanation: Because the void pointer is used to cast the variables only, So pointer arithmetic can't be done in a void pointer.

Can we delete void pointer in C++?

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.

What is void or generic pointer in C?

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.

Is it possible to dereference void pointers?

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:

What is a dereference pointer in C++?

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.

What type of pointer is (void*)0?

(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?

How to cast a void pointer to an int pointer?

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] ...)


3 Answers

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;
like image 131
David Grayson Avatar answered Oct 18 '22 17:10

David Grayson


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]...)

like image 21
teppic Avatar answered Oct 18 '22 17:10

teppic


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.

like image 6
Ed S. Avatar answered Oct 18 '22 18:10

Ed S.