Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I can't understand exactly this paragraph about void pointers.
Pointer of type void which can take address assigned from any other pointer (except function pointer) and can be reassigned to other pointer type. Void pointers cannot be dereferenced. They act as place holder.
can anyone explain it with an example?
thanks
Posted
Comments
Sergey Alexandrovich Kryukov 29-Sep-13 1:56am    
What exactly is unclear here? It point to memory, not to a particular known type. It takes some experience of working with pointers on low level (which is really a simple thing, but by some reason there are too many people apparently nearly incapable of simple programming of this sort). When you get such experience, the value of void pointers becomes pretty obvious.
—SA
Hanoi 2014 29-Sep-13 4:45am    
what does it mean:
except function pointer
and
They act as place holder.
pasztorpisti 29-Sep-13 5:53am    
A pointer is "just an integer" that is an index into the memory address space of your program. Its an "index" into the memory and this "index" tells you the location of some other data in memory that usually has some type. Sometimes this type is unimportant, for example when you call a function that writes out data into a file, in this case the file writer function receives just a void* and the size of data in bytes so you can easily use it with any typed pointer.

Declaring a pointer as typed or untyped is just giving some language dependent information for the compiler on how is the programmer allowed to use the pointer and to make it easier for the programmer to generate code that uses pointers to manipulate complex data (like in case of a pointer to a complex structure). Learn some assembly programming and after that you won't be surprised about this, you will immediately adore typed pointers/references and the work they do for you.
Sergey Alexandrovich Kryukov 29-Sep-13 11:55am    
Right. This is an awkward expression. I would not use the concept of "placeholder" at all, it would be just wrong. This is just the raw pointer. For example it, can point to polymorphous data, and the code can work with abstract data only (arrays of bytes, not using the fact that the bytes are semantically structured). For example, what would you do if you need to copy some piece of memory, without any concern of what's in it? You use raw memory pointed to void pointer.
—SA
pasztorpisti 29-Sep-13 5:44am    
"and can be reassigned to other pointer type"
This is true only in case of C, in C++ a typed pointer can be set using a void pointer only by explicitly casting the void pointer into a compatible typed pointer.

It's pretty simple once you get you head round it:
A void pointer can hold the address of any kind of data:
C++
void* vp;
int* ip = &ai[2];
char* cp = "hello";
vp = ip;
vp = cp;
All are valid.

But since you can't declare a variable as
C++
void v;

You can't "dereferrence" the pointer by trying to access the value it is pointing at:
C++
*vp = i;
DoSomething(vp->member);
Both are illegal.

There are good reasons for this, apart from the understandable lack of a "void" type variable: pointers are not all the same. A pointer to an int can only hold physical addresses which are aligned on a word boundary - because integers are word based values. Char Ballard pointers aren't, because chars are byte aligned.
 
Share this answer
 
Comments
Hanoi 2014 30-Sep-13 6:31am    
Thanks very much for good help
OriginalGriff 30-Sep-13 6:55am    
You're welcome!
CPallini 1-Oct-13 8:21am    
5.
As your "paragraph" the answer and the comments all say, void* is just a pointer to a memory block of any type. Void* can point to memory of any type but void* cannot be dereferenced.

But this is not totally true. Of course you cannot dereference a void*. But you can, if you type-caste it to the type which it points to.

C++
int a;
void *p = &a;

int new_a;
new_a= *p; //this line is wrong
new_a= *(int*)p; //but this works


This is a neat trick and it often helps to implement generic data types in C/C++.
 
Share this answer
 
v2
Comments
Hanoi 2014 2-Oct-13 1:18am    
Thanks you for very good answer,but about this:
new_a = *(int)p;
I can't understand why must use a pointer in typecasting
Captain Price 2-Oct-13 1:35am    
First it's,
new_a = *(int*)p;
Not,
new_a = *(int)p;
<cod>p is a Void*, C/C++ cannot dereference it since C/C++ doesn't know the type of it. But you do know the type of the variable which p points to. So this ; (int*)p, means that you are changing a pointer of type void to a pointer of type int. And this line ; *(int*)p, (notice the asterisk mark in the front) means that you are changing a Void* to int* and then dereferencing the resulted int*. The same piece of code above can be also written as this :


int * new_p = (int*)p;
int new_a = *new_p;


Why you needed to type-cast was because, the compiler doesn't know the type of what p points to. So basically in the above type-case, you're giving some knowledge which the compiler doesn't know.
Hanoi 2014 2-Oct-13 1:50am    
Very good, Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900