What Does Void Mean in C, C++, and C#

What does void mean in C, C++, and C#?

Basically it means "nothing" or "no type"

There are 3 basic ways that void is used:

  1. Function argument: int myFunc(void)
    -- the function takes nothing.

  2. Function return value: void myFunc(int)
    -- the function returns nothing

  3. Generic data pointer: void* data
    -- 'data' is a pointer to data of unknown type, and cannot be dereferenced

Note: the void in a function argument is optional in C++, so int myFunc() is exactly the same as int myFunc(void), and it is left out completely in C#. It is always required for a return value.

What does void Enter() = 0; mean in C#?

That code is not C#, it is C++, and is the equivalent to C#

abstract class MapSite
{
public abstract void Enter();
}

It makes the function pure virtual, and the class abstract. Subclasses will be abstract as well unless they provide a definition for the member function void Enter()

What is void** in C#?

It's a pointer to a pointer to something not specified. Basically, just think of it as a memory pointer to a raw memory pointer.

So, int** is a pointer to a pointer to an int, but void** is a pointer to a pointer, but it's not specified what that pointer is pointing at.

I did some google searches and could only find information about void*, which is a pointer to a sort of catch all top level type, if I understood correctly.

Not quite. void* is a pointer to something, it's just not specified what that something is and should just be thought of as a pointer to a raw hunk of memory that you have to apply some structure to. For example, malloc returns a void* because it's returning a pointer to a raw hunk of memory.

Difference between C++ void Pointer and C# var

The other answers here are pretty good but I think they do not get clearly to the fundamentals. It is the fundamentals you are confused about, so let's address those.

  • A variable is a storage location that contains a value.
  • A variable is associated with a type.
  • A local variable has a name.

So voidInt, voidChar, voidCharArray, varInt, varChar and varCharArray are all variables, and they all have types associated with them. Each variable can be assigned a value of that type or produce a value of that type, depending on whether the variable is being written to or read from.

OK, so now what are pointers?

  • A type has a corresponding pointer type. (Note that in unsafe C# only the unmanaged types have corresponding pointer types.)
  • The void * type is a special pointer type.
  • A pointer is a value.
  • A pointer of type T* may be dereferenced to produce a variable of type T. T* must not be void*.
  • A pointer may be explicitly converted to or from any integral type, though these operations are permitted to lose information and are dependent on implementation details.
  • Any pointer value may be implicitly converted to void*.
  • Any void* value may be explicitly converted to any pointer type value.

And what is var in C#?

  • var is a "syntactic sugar" that tells the compiler to deduce the type of the variable from the initialzier rather than requiring that it be written out.

And what are "anonymous types" in C#?

  • Some expressions in C# have a type that is not declared and has no name; these are known as the "anonymous" types.

So now we can look at your program and see what each line does.

void * voidInt = (void *) 7;

voidInt is a variable of type void*. The value assigned to it is the conversion of the integer 7 to a pointer, which is almost certainly a garbage pointer on any modern operating system. This code is essentially nonsensical.

More sensible code would be:

int myInt = 7;
int* intPtr = &myInt;
void* voidInt = intPtr;

This means that myInt is a variable which holds the value 7, intPtr is a variable which holds a pointer; when that pointer is dereferenced it produces variable myInt. voidInt is a variable which holds any pointer, and the value read from intPtr is a pointer. So now voidInt and intPtr both hold a pointer to variable myInt.

void * voidChar = (void *) 'F';

Same thing here. The character F is treated as a number and converted to a pointer value, which is stored in the variable. This is not sensible. Sensible code would be something like:

char myChar = 'F';
void *voidChar = &myChar;

But this makes perfect sense:

void * voidCharArray = (void *) "AbcString";

A string literal in C++ is convertible to a char* which is a pointer to the storage for the first character, and that pointer is convertible to void*.

What about this?

var varInt = 7;
var varChar = 'F';
var varCharArray = "AbcString";

This is just a pleasant way to write

int varInt = 7;
char varChar = 'F';
string varCharArray = "AbcString";

Each variable has its given type, and each assignment stores a value of that type in the variable.

What about anonymous types?

var anon = new { X = 123, Y = 456 };

This makes a variable of anonymous type, where the anonymous type has two properties X and Y both of type int. The type has no name, so there is no way to write out the type in the declaration, hence var must be used.

The key thing here is to make sure that you have a grasp of the fundamentals: pointers are values, they may be dereferenced, and doing so produces a variable. Since pointers are values they may themselves be stored in variables of pointer type. This has almost nothing to do with var, which is a pleasant way in C# to make the compiler do the work of figuring out what type a variable should have.

C#:: What is the difference between int main(){...} void main(){...} int main(void){...} void main(void){...}

1- function uses to return an integer number

int main()
{
return 1;
}

so if you call this function like this:

int x = main();

result of x will be "1"

2- void function does n't return any value

void main()
{
Console.WriteLine("Hello World");
}

so you can call this function like this:

void main();

this just execute "void main" function and would not return anything

Is 'void' a valid return value for a function?

void is not an actual return (data)type! void says there is no result. So you can not return a value in a method that's declared void even though the method you're calling is also declared void.

I must admit it would be a nice shortcut, but it's not how things work :-)


Just an additional thought: If what you want was allowed, void would become both a data type and also the only possible value of that data type, as return x; is defined as returning the value x to the caller. So return void; would return the value void to the caller - not possible by definition.

This is different for null for example, as null is a valid value for reference types.



Related Topics



Leave a reply



Submit