Pointer is very interesting feature of C language. With the help of the pointers we can perform low level operations like memory management in C language which are impossible without pointers.
Pointers are the variables that stores memory address of another variable.
Features of Pointers in C
- It helps in the dynamic memory management.
- It stores the memory address of another variable.
- It provides a way to return more than one value to the function.
- Reduces the execution time of the program.
- Provides an alternative way to access array elements.
Declaration of pointer variable
To declare pointer variable is similar as declaring any other variable except we need to provide asterisk (*) symbol before the variable name.
Syntax
type *variable_name;
Here type can be any data type and variable_name should be a valid identifier.
Example
int *var;
float *myNumber;
char *ch;
Using pointers in program
To use pointer in a program we need to follow following steps:
- Declare the pointer variable
- Assign address of a variable to pointer variable
- Access the value that the pointer variable is pointing
Program example of pointers in C
#include<stdio.h>
int main()
{
int var = 10;
int *ptr;
ptr =&var;
printf("Actual variable = %d\n", var);
printf("Address stored on pointer variable is %d\n", ptr);
printf("value that the pointer pointing is %d\n", *ptr);
return 0;
}
output
Actual variable = 10
Address stored on pointer variable is 0x7fff50ac0aa8
value that the pointer pointing is 10