Need of defining datatype of a pointer in C

Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage. Example: int 2/4 bytes, char 1 bytes, short 2 bytes

The Data type is needed when dereferencing the pointer, so it knows how much data it should read. For example, dereferencing a char pointer should read the next byte from the address it is pointing to, while an integer pointer should read 2/4 bytes (depends on machine 16/32 bit).

Since, we have used the term dereferencing, we need to know what is it.

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator. It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer.


#include <stdio.h>  
int main() {  
	int a=10;   //we declare the integer variable to which the pointer points
	int *ptr;   //we declare the integer pointer variable. 
	ptr=&a;     //we store the address of 'x' variable to the pointer variable 'ptr'
	*ptr=8;     //value of 'x' variable is changed to 8 by dereferencing a pointer 'ptr'
	printf("Value of x is : %d", a);  
	return 0;

Value of x is : 8

The significance of datatype of a pointer:-
1. The compiler has to know the size of the memory cell, the pointer is pointing to.
2. Without the datatype the safety cant be assured.
3. When accessing the structures from the pointer the typecasting of every pointer is must.

You Missed

Also Checkout