Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is void* and to what variables/objects it can point to

Tags:

Specifically, can it point to int/float etc.? What about objects like NSString and the like? Any examples will be greatly appreciated.

like image 975
SNR Avatar asked Feb 24 '11 12:02

SNR


People also ask

What does a void pointer point to?

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.

What are void variables?

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.

What does void * do in C?

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.

What does void * func () mean?

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.


1 Answers

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.

like image 184
Armen Tsirunyan Avatar answered Sep 20 '22 12:09

Armen Tsirunyan