Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Void type !

I'm was ignoring this for longtime but I see it's time to ask what it's could be used for ?

what I can do with a void variable ? when I print it out it gives me same result if it's 1 pointer or 2 or 3 and so on ... also I tried to assigne to it any type it refuses , it accept only this : "NULL" , what does it used for ?
[350 byte] By [Amahdy] at [2007-11-11 10:02:12]
# 1 Re: Void type !
Generally you use them as pointers when you want to be able to pass different types through a high level function. This is left over from C and is mostly avoidable via OOP constructs in C++.

void * vp = &anything;
...

if(it was an int pointer, for example)
real_pointer = (int*) vp;

etc.

One example is the .net thread function afxbeginthread and its friends. They use a void pointer as a pass through arguement list to the function that is being threaded, which could be *any* function, with any number of parameters, etc.
jonnin at 2007-11-11 20:59:37 >
# 2 Re: Void type !
You're confusing two concepts: void and void*. The former means 'no parameter at all' for example, when it's used as the return type of a function that doesn't return any value.
void* is a generic pointer. You can assign any data pointer to void *, and use void* when the function expects various pointer types. The actual type is then recovered by some other means. Anyway, void* is less useful in C++ than in C. In C++ you use templates for generic code.
Danny at 2007-11-11 21:00:37 >
# 3 Re: Void type !
And this will hold more in arrays for example ? : void** ??
Amahdy at 2007-11-11 21:01:41 >
# 4 Re: Void type !
you can declare an array of void*. The problem however is that you need to uncover the actual type of the pointers that are disguised as void. For example, an array of void* can contain int *, string*, etc. The actual types have to be encoded somehow in the application (say by using type codes) because void* itself doesn't give you much. You need to cast it back to the original type before you can access the datum to which it points.
Danny at 2007-11-11 21:02:42 >
# 5 Re: Void type !
Danny of corse I mean the mentioned in the first post with pointer .. any way how "You can assign any data pointer to void *, " or do u mean the casting listed in jonnin answer ?
Amahdy at 2007-11-11 21:03:41 >
# 6 Re: Void type !
ok Danny , and it's in the internal machine ocuppie what ? WORD or what ? and the pointer of void* how it will be formed .. I see it's unusefull but we still learning and I love know more .
Amahdy at 2007-11-11 21:04:34 >
# 7 Re: Void type !
void* is represented as any data pointer. The precise size is machine dependent however in 32-bit machines it's usually implemented as DWORD. 64-bit machines use unsigned long long. There are other restrictions though. For example, most modern OSs don't permit pointers that have small values such as 12, 100 etc.
Danny at 2007-11-11 21:05:38 >
# 8 Re: Void type !
Ok thanks Danny and jonnine for information .

Regards.
Amahdy at 2007-11-11 21:06:37 >