Pointers

The pointer is a variable which contains the address of another variable.

We can't assign a value to a pointer. Instead, we can assign the address of another variable to the pointer.




Why pointers?

Using pointers, we can directly communicate with the hardware. This is very useful in embedded programming. In embedded programming, we read and write data into hardware address whereas normal c programming we read data from the user and we will show output to the user.


1.Reading temperature information from the temperature sensor. This temperature sensor chip will have its own device memory where it will store the current temperature value.

We can read the temperature value from the sensor device memory using pointers. Basically, we will assign device memory address to a pointer variable. using the pointer variable, we can directly read and write data to the device memory.

2.If we pass data to some function, it will duplicate the data and send one copy to the function. Instead, we can pass the address of a variable i.e pointer. if we pass the pointer to the function, there will not be any data duplication. So, our program will run much faster.

3.Using pointers, we can dynamically allocate the memory.

In runtime, we can create, resize, deallocate the memory based on our requirement.




Syntax

data_type *pointer_name;

Example

int    *iptr; //iptr is a pointer to type int
char   *cptr; //cptr is a pointer to type char 
float  *fptr; //fptr is a pointer to type float 
double *dptr; //dptr is a pointer to type double 

Note

An integer pointer only holds the address of an integer variable.

A character pointer only holds the address of a character variable.

Similarly for, float and double.