Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have pointers other than void

Tags:

c

pointers

I know that we have different pointers like int, float, and char. A void pointer is the only pointer which can hold all others.

Do the other pointers exist only for the flexibility to do pointer arithmetic?

Is there any other reason that pointers other than void are present in C language?

like image 392
Vijay Avatar asked Feb 04 '10 14:02

Vijay


People also ask

What is the purpose of pointers?

Pointers save memory space. Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location. Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well.

Why do we have different types of pointers?

There are several reasons: Not all addresses are created equal; in particular, in non Von Neuman (e.g. Harvard) architectures pointers to code memory (where you often store constants) and a pointers to data memory are different.

Why do we need void pointers?

Why do we use a void pointer in C programs? We use the void pointers to overcome the issue of assigning separate values to different data types in a program. The pointer to void can be used in generic functions in C because it is capable of pointing to any data type.

What is the limitation of void pointer?

The only limitations with void pointers are: you cannot dereference void pointer for obvious reasons. sizeof(void) is illegal. you cannot perform pointer arithmetics on void pointers.


1 Answers

Type safety. Defining the type of pointers helps the compiler find errors where you are trying to use data of the wrong type through a pointer. That's the reason C has types in the first place.

like image 115
Max Shawabkeh Avatar answered Oct 04 '22 08:10

Max Shawabkeh