Specifically, can it point to int/float etc.? What about objects like NSString and the like? Any examples will be greatly appreciated.
The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer.
A variable or field with type void will be removed from the program by the compiler. It cannot be assigned to a variable of any non-void type, and non-void values cannot be assigned to a void variable. A void variable cannot be used in any context that requires a non-void value.
void (C++) If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword. A void* pointer can't be dereferenced unless it's cast to another type.
Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.
void*
is such a pointer, that any pointer can be implicitly converted to void*
.
For example;
int* p = new int; void* pv = p; //OK; p = pv; //Error, the opposite conversion must be explicit in C++ (in C this is OK too)
Also note that pointers to const cannot be converted to void*
without a const_cast
E.g.
const int * pc = new const int(4); void * pv = pc; //Error const void* pcv = pc; //OK
Hth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With