Explain the concept of pointers in C language



The pointer is a variable that stores the address of another variable.

Features of Pointers

  • Pointer saves the memory space.

  • The execution time of a pointer is faster because it directly accesses to memory location.

  • The memory is accessed efficiently with the help of a pointer.

  • Memory is allocated and deallocated dynamically.

  • Pointers are used with data structures.

The syntax for the pointer is as follows −

pointer = &variable;

Example

Following is the C program for the pointer −

 Live Demo

#include <stdio.h>
int main(){
   int x=40; //variable declaration
   int *p; //pointer variable declaration
   p=&x; //store address of variable x in pointer p
   printf("address in variable p is:%d
",p); //accessing the address    printf("value in variable p is:%d
",*p); //accessing the value    return 0; }

Output

When the above program is executed, it produces the following result −

Address in variable p is:5ff678
Value in variable p is:40

Operator * serves two purposes which are as follows −

  • Declaration of a pointer.

  • Returns the value of the referenced variable.

Operator &  serves only one purpose, which is as follows −

  • Returns the address of a variable.


Advertisements