Goseeko blog

What are Pointers?

by Bhumika

One of the vital and heavily used features of ‘C’ is pointer. Most of the other programming languages also support pointers but only few of them use it freely. Support pointers but only few of them use it freely. 

When we declare any variable in C language, there are three things associated with that variable. 

  1. Data type of variable : Data type defines the type of data that variable can hold. Data type tells the compiler about the amount of memory allocated to the variable.
  2. Address of Variable : Address of variable represents the exact address of memory location which is allocated to variable.
  3. Value of variable : It is the value of variable which is stored at the address of memory location allocated to variable.

Example :      

int  n = 5;

In the above example ‘int’ is the data type which tells the compiler to allocate 2 bytes of memory to variable ‘n’.

Once the variable is declares the compiler allocate two bytes of memory to variable ‘n’. Suppose the address of that memory location is 1020. At the address of memory location 1020 the value 5 is store.

Memory map for the above declaration is as follows. 

use of &,/and ‘*’ operator

Program for Pointers

Consider the following program. 

#include <stdio.h>

#include <conio.h>

void main()

{

int a,b,c;

printf(“Enter two numbers”);

scanf(“%d%d”, &a,&b);

c = a+b;

printf(“/n Addition is %d”, c);

printf(“/n Addition of variable  is %d”, &c);

getch();

}

The above program is the program for the addition of two numbers in the ‘C’ language. In the seventh instruction of the above program, we used operator ‘%’ and ‘&’ ‘%d’ is the access specifier which tells the compiler to take an integer value as an input whereas. ‘&a’ tells the compiler to store the taken value at the address of variable ‘a’.

In the ninth instruction, the compiler will print me the value of ‘C’ so the output of the 9th instruction is as follows.

Addition is 5    [Note : considering a = 2 and b = 3].

In the tenth instruction, the compiler will print me the address of variable C.

So the output of the length instruction is as follows. The address of variable C is 1020.

Interested in learning about similar topics? Here are a few hand-picked blogs for you!

You may also like