Initialization and Dereferencing of Pointer Variable in C Language with Free Notes

Initialization of Pointer

Pointer variables can be initialized as:

pointer = &variable;

Initialization of pointer variable can be done as:

  • Declare a Pointer variable and note its data type.
  • Declare another variable with same data type as that of pointer variable.
  • Initialize ordinary variable and assign some value to it.
  • Now initialize pointer by assigning the address of ordinary variable to pointer variable.
Free E-Books and Notes

For Example:

#include<stdio.h>
void main()
{ 
   int a;       //Step 1
   int *ptr;    //Step 2
   a = 10;      //Step 3
   ptr = &a;    //Step 4
}

Example of Initializing Integer Pointer

#include<stdio.h>
void main()
{ 
   int a = 10;      
   int *ptr;       
   ptr = &a;   
     printf("\n Value of ptr: %u", ptr); 
}

Output

Value of ptr: 2002

Dereferencing Pointer Variable

  • For dereferencing the pointer variable, Asterisk (*) indirection operator is used along with pointer variable.
  • Asterisk Operator is also called as value at address operator.
  • When it is used with pointer variable, it refers to variable being pointed to, and is called as Dereferencing of Pointers

For Example:

int a = 10, x;
int *ptr;
ptr = &a;
x = *ptr;

It can be evaluated as:
*(ptr) = value at (ptr)
       = value at (2002)
       = 10

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top