void*

Hi,
I have a function like so:
void Foo(void* buffer)
So i have a structure of type unsigned int* i want to pass to it...
Do i have to cast it? How do i call this?
Thx
[197 byte] By [Clarke Kent] at [2007-11-17 11:50:12]
# 1 Re: void*
Well, you should not have to cast it, a pointer is a pointer.

unsigned int * poo;
void Foo(void* buffer);

// give poo a valid pointer value then call it

poo = 12;
Foo( poo );

should work.
JohnDavidHanna at 2007-11-10 8:06:54 >
# 2 Re: void*
In this case you do not need to cast explicitely it will be done implicitely. Nevertheless you can do it to clarify your code...

1. Implicit cast

unsigned int *pPointer = NULL;

// Initialize 'pPointer' etc.

// Call function
Foo(pPointer);

2. Explicit cast

unsigned int *pPointer = NULL;

// Initialize 'pPointer' etc.

// Call function
Foo(static_cast<void *>(pPointer));

Ciao, Andreas

"Software is like sex, it's better when it's free." - Linus Torvalds
Andreas Masur at 2007-11-10 8:07:56 >
# 3 Re: void*
Hi,

void* means nothing but a pointer to unknown type. So you can pass any pointer and you will not get any compile error. But this doesn't give you any guarantee that it will work at runtime unless you send the pointer the function is expecting..
for eg if the function Foo is written in the following way...

void Foo(void* buffer)
{
::MessageBox(0, (char*) buffer, 0, 0);
}

And you call the function the following way...

int iVal = 410;
Foo(&iVal);

The above statement doesn't give any syntax error(compile error), but at runtime it may crash! So you need to know the purpose of any parameter before passing. Hope this helps!!!

Let me know if got helped. If you got helped pls rate!!!

John
Indian_Techie at 2007-11-10 8:08:54 >