Can you dereference a void pointer
It would be incorrect, if we assign an address of a float variable to a pointer of type pointer to int. But void pointer is an exception to this rule. A void pointer can point to a variable of any data type.
Here is the syntax of void pointer. Here vp is a void pointer, so you can assign the address of any type of variable to it.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Dereference void pointer Ask Question. Asked 8 years, 8 months ago. Active 2 years, 10 months ago.
Viewed 78k times. Improve this question. Anthony Saurabh Ghorpade Saurabh Ghorpade 5 5 gold badges 13 13 silver badges 21 21 bronze badges. You can't dereference a void pointer. What's the actual compilation error?
In four lines of code we have a an unnecessary cast, b an illegal dereference of an untyped pointer, and c out-of-bounds undefined-behaviour. What compiler errors do you get? It's difficult to tell you the reason for errors without the actual errors themselves.
Code-Guru: In general, yes. Here, not so much. What do you expect lVptr[1] to return? Show 2 more comments. Active Oldest Votes. Improve this answer.
David Grayson David Grayson Why does the compiler have to care about interpreting memory? Isn't that up to the operations I manipulate that memory with? Or even just to read some memory, you need to specify a type so the compiler knows how many bytes to read. In the above code, we notice that since ptr is a void pointer, we can make it point to a variable of char type as well as a variable of int type.
It can point to any type of variables. Some important points to remember while using void pointers:. What is void pointer in C? Written By - Namrata. What are void pointers in C? Some important points to remember while using void pointers: A void pointer cannot be dereferenced.
0コメント