Declaring, Initializing and using a pointer variable

In this tutorial, we will learn how to declare, initialize and use a pointer. We will also learn what NULL pointer are and where to use them. Let's start!


Declaration of Pointer variable

General syntax of pointer declaration is,

datatype *pointer_name;

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. void type pointer works with all data types, but is not often used.

Here are a few examples:

int *ip     // pointer to integer variable
float *fp;      // pointer to float variable
double *dp;     // pointer to double variable
char *cp;       // pointer to char variable

Initialization of Pointer variable

Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of a variable of the same data type. In C language address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it.

#include<stdio.h>
    
void main()
{
    int a = 10;
    int *ptr;       //pointer declaration
    ptr = &a;       //pointer initialization
}

Pointer variablea always point to variables of same datatype. Let's have an example to showcase this:

#include<stdio.h>

void main()
{
    float a;
    int *ptr;
    ptr = &a;       // ERROR, type mismatch
}

If you are not sure about which variable's address to assign to a pointer variable while declaration, it is recommended to assign a NULL value to your pointer variable. A pointer which is assigned a NULL value is called a NULL pointer.

#include <stdio.h>

int main() 
{
    int *ptr = NULL;
    return 0;
}

Using the pointer or Dereferencing of Pointer

Once a pointer has been assigned the address of a variable, to access the value of the variable, pointer is dereferenced, using the indirection operator or dereferencing operator *.

#include <stdio.h>

int main()
{
    int a, *p;  // declaring the variable and pointer
    a = 10;
    p = &a;     // initializing the pointer

    printf("%d", *p);    //this will print the value of 'a'

    printf("%d", *&a);   //this will also print the value of 'a'

    printf("%u", &a);    //this will print the address of 'a'

    printf("%u", p);     //this will also print the address of 'a'

    printf("%u", &p);    //this will print the address of 'p'
    
    return 0;
}

Points to remember while using pointers:

  1. While declaring/initializing the pointer variable, * indicates that the variable is a pointer.
  2. The address of any variable is given by preceding the variable name with Ampersand &.
  3. The pointer variable stores the address of a variable. The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable storing integer value.
  4. To access the value of a certain address stored by a pointer variable, * is used. Here, the * can be read as 'value at'.

Time for an Example

#include <stdio.h>

int main()
{
    int i = 10;     // normal integer variable storing value 10
    int *a;     // since '*' is used, hence its a pointer variable

    /*
        '&' returns the address of the variable 'i' 
        which is stored in the pointer variable 'a'
    */
    a = &i;

    /* 
        below, address of variable 'i', which is stored 
        by a pointer variable 'a' is displayed
    */
   printf("Address of variable i is %u\n", a);

    /*
        below, '*a' is read as 'value at a' 
        which is 10
    */
   printf("Value at the address, which is stored by pointer variable a is %d\n", *a);

   return 0;
}

Address of variable i is 2686728 (The address may vary) Value at an address, which is stored by pointer variable a is 10