Introduction to Pointers

A Pointer is a special variable that stores the address of another variable rather than storing the values. Thus, with the help of pointers, any variable can be accessed by its address.

Accessing a variable by its address is faster than accessing it by its name as the compiler is to first get the address of that variable and in the second cycle need to access that address.

Pointers are used to access memory and manipulate the address.

What does address of a memory location mean?

Whenever a variable is defined in C language, memory is allocated to that variable in which the value will be stored. The address can be checked with the ampersand(&) symbol.

If var is the name of the variable, its address can be fetched with &var.

Example:

#include <stdio.h>

int main ()
{
  int var = 5;
  printf ("\n The memory address of the variable var is: %x\n", &var);
  return 0;
}

Output:

The address of the variable var is: 15e490ac

Concept of pointers:

Whenever a variable is defined in a program, a memory address is allocated which holds the value. This address can be accessed and manipulated by pointers.

Since the memory address is also a number, they can be placed in some other variable. The variables that hold the memory address are known as pointers.

Hence, a pointer is nothing but a variable which holds the address of some other variable and even this variable will have its own address.

Advantages of Pointer:

In the next section, we will be covering the declaration, initializing, and using the pointer variable in C.

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: