what does void mean!?

whitesaint

cocoa love
okay reading all the cocoa books and crap, i know that a "void" function doesn't return anything. Well i just found out that you can declare variables using "void". Like yknow:

float aFloat;
void aVariable;

what does "void" mean when you declare a variable with it? All help is welcome and appreciated, thanks.

-whitesaint
 
'void' basically means that the variable is untyped. Generally, you use it in variable declarations when you're passing a pointer to a function and the function doesn't need to have any knowledge of what the variable points to. So, instead of having the function just pick some arbitrary type, forcing the caller to coerce the pointer to that type like this
Code:
void MyFunction(char * somePointer)
   (does something that doesn't depend on what somePointer is pointing to)
...

int *intPointer;
MyFunction( (char *)intPointer );
you can use void like this
Code:
void MyFunction(void* somePointer)
   (does something that doesn't depend on what somePointer is pointing to)
...

int *intPointer;
MyFunction( intPointer );
But you probably wouldn't want to declare a variable of type 'void.'

Hope this helps - I'm kinda tired, so it may not have made much sense :eek:
 
I think void in a return statement means nothing and void* as a type essentially means anything. Kind of a strange convenetion.

F-bacher
 
'void' is only used for functions which don't return a value, and 'void *' (void pointers) are used to point at generic blocks of data, which the function currently moving/dealing with it doesn't know what it really is.

So if I have a function that allocates a block of data for me out of a pool, it will probably be passing back a void pointer, which I typecast into the desired type, such as an integer pointer, a float pointer, or structure pointer.

It is one of those things that is there, but isn't really used all THAT often by high-level programmers.
 
it's one of the reasons I hate gcc.

I wrote ansi C code in CodeWarrior, along with a couple of the most intelligent computer people I've ever known. The assignment had to be turned in on Solaris, so we ftp'd the files over, and gcc insisted there was a type mismatch. The only solution we found was to type our variable as void, stopping all type checking from occurring. Then it worked fine. Stupid gcc. Stupid hack.

In Obj C, you'd rather use id instead of void when defining a variable. But really, id is aliased to void as far as gcc is concerned, so it skips ALL type checking at compile time.
 
Actually, Codewarrior is a little loose when it comes to the ANSI standard. You are supposed to typecast when using malloc() (since it returns a void * or char * depending on your system), or when doing ANY conversion so that it knows how to properly translate the data.

Codewarrior is actually the one bending the ANSI C spec, not gcc.
 
Back
Top