Pointers in C Language

Pointer is a derived data type in c language. It can store the address of memory where program instructions or data are stored.

Pointers in C Language
Pointers in C Language

Pointers

Pointer is a derived data type in c language. It can store the address of memory where program instructions or data are stored.
For example:- if  int a=5; then pointer stored the address of "int a" (ptr= &a).

Note: In C language "&" is an address operators.

Importance of pointers

  • Pointers can be used to return multiple values.
  • Pointers allow to manage the dynamic memory.
  • Pointers provide efficient tool for manipulating dynamic data structure.

Declaration of pointers

data_type *ptr_name;

Styles
  1. Int*  ptr;
  2. Int  *ptr;
  3. Int  * ptr; 
Initialization of pointers

Int quantity;
Int *ptr; // declaration 
ptr = &quantity;   // initialisation 

Program:
#include<stdio.h>
int main()

   int a = 5;
   int *ptr;
   ptr = &a;
    printf("%u",ptr);
return 0;
}

Chain of pointers

A pointer can also stored the address of other pointer.

Rule of pointer

There are following rules:
  • A pointer variables must be assigned the address of  another variable.
  • A pointer variable can be initialized with NULL or zero.
  • An integer value may be added or subtracted from a pointer variable.

Array pointers 

When the pointer is store the address of an array.
Program:
#include<stdio.h>
int main()

   int arr[5] = {1,2,3,4,5};
   int *ptr;
   ptr = &arr;
   printf("%u",ptr);
return 0;
}

Post a Comment