Declaring, Initializing and Dereferencing a pointer varaible

In this article we will be learning how to declare a pointer, initialize and use a pointer variable.

Please refer to the topics below for a better understanding of the section below:

Declaration of pointer variable:

Like any other variable, a pointer variable needs to be declared before its use.

Syntax:

data_type *pointer_name;

The data type of the pointer and the variable to which the pointer variable points must be the same. If not, we need to do typecasting.

Initialization of pointer variable:

Pointer initialization is nothing but assigning value to the pointer variable. It contains the address of a variable of the same data type. The ampersand(&) operator can be used to get the address of a variable.

Example:

#include <stdio.h>

int main ()
{
  int var = 5;
  int *ptr; // Pointer declaration
  ptr = &var; //Pointer initialization

  printf ("\n The value at address: %p is: %d\n", ptr, *ptr);
  return 0;
}

Output:

The value at address: 0x7ffd33bd5134 is: 5

Pointer variable always points to a variable of the same data type. For example:

float var =10.6;
int *ptr = &var; // assignment from incompatible pointer type because of 'int' and                         'float'

NULL Pointer:

While declaring a pointer variable, if it is not initialized/assigned to anything, it can point to some random memory location, hence contains garbage value. There it is recommended to assign a NULL value to it.

The pointer that is assigned a NULL value is called a NULL pointer in C.

int *ptr = NULL;

Dereferecing of pointer:

Once the pointer variable is initialized to address a variable, the process of fetching the value of that variable is known as dereferencing the pointer.

Dereferencing is done with a special operator in C known as indirection or dereferencing operator(*).

Example:

#include <stdio.h>

int main ()
{
  int var = 10;
  int * ptr = &var; //Pointer declaration and initializing
  
  /* Dereferencing  of pointer */
  printf ("\n The value of the pointer variable is: %d\n", *ptr);
  return 0;
}

Output:

The value of the pointer variable is: 10

Note:

  • While declaring/initializing the pointer variable, * indicates that the variable is a pointer.
  • The address of any variable is fetched by preceding the variable name with ampersand &.
  • The pointer variable stores the address of the variable.
  • The declaration int *var does not mean that var is going to contain an integer value. This means var is a pointer variable that is going to store the address of a variable of integer type.
  • To assess the value of certain addresses stored by the pointer variable * is used. This is called dereferencing.

Relevant Posts:



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: