Please clarify my understanding of the data type 'void'

econjack: @michinyon: It's not terrible...it's wrong! This should be a little better:

void * function()
{
  char c;
  char *ptr;
  
  ptr = &c;
  *ptr = 'A';
  return ptr;
}

And what happens when the function ends and char c passes out of scope?

@GoForSmoke: The initial point of this thread was to show what void meant. All I was trying to demonstrate was that a function that returns a void is used for type checking. That part is correct. All I was doing with the function was showing that type checking is performed on a void pointer, as the compiler throws an error if the return pointer is not cast. I don't really care what happens to c when it goes out of scope. What I do care about is that the original posted realizes that void does have a purpose...a purpose the compiler checks.

Function does not return 'a void'. It returns void meaning nothing.

You can have a void pointer but you cannot have a void.

Void is undeclared. No heap is allocated. Nothing is put on the stack. A void point is an undeclared pointer that can be cast into any other pointer.

Not having type void would be like a number system that doesn't have zero.

I think it is misleading and unhelpful to describe "void" in a function declaration/definition as a "data type".

It is not a data type. It is a keyword that stands in the syntactic place of a datatypename ( from the point of view of parsing/compiling the source code ), to indicate that there is no data requiring to be data-typed.

As for void* , thats a completely different thing. A variable that is declared with the datatypename void* isn't void and it doesn't point to anything void. It is a pointer variable which can be null, or is able to contain the address of a memory location which contains an object or data of some time not currently known, or not relevant in the context where the variable is declared. As the other's have said, it's just a pointer.

void pointer is an address where all the other pointers are address and type compiled. So you tell the compiler if it’s void just as you would for int.

You seem to have functions confused with arrays. Or maybe you enjoy writing functions that do nothing whatsoever?

A function that returns void returns nothing. The return is left on the stack by the ending function for the calling function to pick up. Void says there is nothing left on the stack, int says there is an int left on the stack.

[/quote] I wish I'd never used that bad example :~. Next time I guess I'll use a functioning bit of code.

michinyon: I think it is misleading and unhelpful to describe "void" in a function declaration/definition as a "data type".

It is not a data type. It is a keyword that stands in the syntactic place of a datatypename ( from the point of view of parsing/compiling the source code ), to indicate that there is no data requiring to be data-typed.

As for void* , thats a completely different thing. A variable that is declared with the datatypename void* isn't void and it doesn't point to anything void. It is a pointer variable which can be null, or is able to contain the address of a memory location which contains an object or data of some time not currently known, or not relevant in the context where the variable is declared. As the other's have said, it's just a pointer.

This has been very helpful, thanks.

I will leave void rest for now. My understanding is better than before, but I have realized over this thread, that a big problem is that I don't know enough terminology, which I should sort out before trying to ask too many more questions :)

Thankyou all for your input, I will keep chugging along and asking more questions. I'll have a look at this thread again once I'm further into coding and see if I get more out of it.

http://arduino.cc/en/Tutorial/Foundations http://arduino.cc/en/Tutorial/HomePage http://arduino.cc/en/Reference/HomePage http://arduino.cc/en/Reference/Libraries http://www.nongnu.org/avr-libc/user-manual/modules.html

Those should help you get started or at least give you more google search words. Don't forget "tutorial".

It is incorrect to think that void is not a type, take a simple proof:

typeof( void ) loop(){}

Void is an incomplete type and is syntactically similar to this:

struct void;

Types are ranges, void's range is simply unknown.

You can still create pointers to incomplete types, take this example:

struct MyVoid;

void loop(){
  MyVoid *m;  //No error.
  MyVoid obj; //Error.
}

Also pointers are types themselves, not just pointers to types.

While the compiler might think of void as a type, I don't have to!

You also cast to void when you want to let the compiler (or lint) know that you really don't care about the return of the function. For example, you will often see (void)sprintf() as a signal that I really don't need to know the number of characters converted, which is what sprintf() returns.

The C++ way is:

static_cast< void >( sprintf() );

Regardless of your desire to ignore it or not, both of our examples are called type casts!

Did I say “ignore”?

I said I don’t have to think of it as a type.

Unlike the compiler, I am a human and can believe two contradictory things at the same time!

“Why sometimes I’ve believed 6 impossible things before breakfast!”

Yeah but did you fix those six bugs before lunch?