Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Pointers

C Programming Language Pointers

Pointers are one of the most prominent features in C & C++ programming. Programmers are always suggested to learn pointers because it teaches how to deal with memory in a very easy & efficient way.
You might have heard that pointers are very difficult to learn for beginners, but it is completely false.
Actually, pointers are very interesting & easy to learn.
Pointers are widely used in C & C++ programming.
We will help you to learn pointers step by step in the very easy way so that in the end you will be able to understand & create any program containing pointers.
Before jumping directly to the pointers concept, let’s see some basic concepts that are very much necessary to understand the pointers.

Memory address/location

Memory address/location is the place in the memory where the actual value is stored.
Value can be of any type like integer, float, char, etc.
In C & C++ programming, memory is represented in the forms of the bytes.
For example, on a 16-bit machine, any integer value uses 2 bytes & on a 32-bit machine, any integer value uses 4 bytes.
On 32-bit machine range of integer value is greater than the 16-bit machine.

Note

We are considering the 32-bit machine to run our pointers program.

Variable name

The variable name is the name assigned to a memory location.
The data type of variable decides the number of bytes allocated to store the required value.
Using variable name we can store a required value at the specific memory location.

For example

    int i;
    i=5;

Here i is the variable name of type integer, and 5 is the integer value which is stored in variable i, that is at memory location whose name is i and allocates 4 bytes.

In C programming, the variable name is always declared at the starting of a program.

C Programming Language Value stored in Memory

Now you might have a question that, through pointers, if we are going to deal with the memory location, then we must be able to know the memory location of any variable.
Yes, of course, we can know memory location of any variable defined in C programming, but how?
It’s very simple, using the address operator (also known as reference operator).
Address operator is represented by symbol ‘& (ampersand).

Let’s take a small example for better understanding

#include <stdio.h>
int main()
{
    int i;
    i = 5;
    printf("Value of i = %d",i);
    return 0;
}

Output

Value of i = 5

Here we can print the value of i. That is the value stored at memory location whose name is i, but not the address of memory location.

Printing address of memory location is very much similar to the printing the value of a variable. For that we just need to use address operator ‘&’ along with the variable name in printf() function, as shown below:

#include <stdio.h>
int main()
{
    int i;
    i = 5;
    printf(" Value of i = %d",i);  // prints the value of i
    printf("\n Address of i = %d",&i);  // prints the address of i
    return 0;
}

Output

Value of i = 5
Address of i = 2293324

C Programming Language variable stored in memory in Pointer image1

We can also obtain/retrieve the value stored at certain address/memory location by using an asterisk (*) sign with address operator ‘& (ampersand) and variable name, as shown below.

Example no 1

#include <stdio.h>
int main()
{
    int i = 5;
    printf(" Address of i = %u",&i);    // display address of i
    printf("\n Value of i = %d",*(&i)); // display value of i using asterisk sign
    printf("\n Value of i = %d",i);     // display value of i
    return 0;
}

Output

Address of i = 2293324
Value of i = 5
Value of i = 5

Example no 2

C Programming Language variables stored in memory in Pointer image2

#include <stdio.h>
int main()
{
    int i = 7;
    int j = 11;
    int k = 3;
	
    printf("\n Address of i = %u",&i);
    printf("\n Address of j = %u",&j);
    printf("\n Address of k = %u",&k);
    printf("\n");
    printf("\n Value of i = %d",*(&i));
    printf("\n Value of i = %d",i);
    printf("\n");
    printf("\n Value of j = %d",*(&j));
    printf("\n Value of j = %d",j);
    printf("\n");
    printf("\n Value of k = %d",*(&k));
    printf("\n Value of k = %d",k);
    return 0;
}

Output

Address of i = 2293324
Address of j = 2293328
Address of k = 2293332

Value of i = 7
Value of i = 7

Value of j = 11
Value of j = 11

Value of k = 3
Value of k = 3

We can assign the address of one variable to another variable as shown below:

Example

C Programming Language variables stored in memory in Pointer image3

#include <stdio.h>
int main()
{
    int i,j = 4;
    i = &j; // assigning address of variable j to variable i, as a value of i
    printf(" Address of j = %u",&j);
    printf("\n Value of i = %d",i);
    return 0;
}

Output

Address of j = 2293328
Value of i = 2293328

Now variable i contain the address of variable j. That is the value of i is the address of j.
But i is a simple variable not a pointer variable.
In C programming, pointer variable is used to store the address of another variable because it helps us to manipulate data very easily using an address of memory locations.

Pointer Variable

A variable that is used to store the address of another variable is known as pointer variable.
It is known as a pointer because it points to another variable using the address.

Syntax

data_type *pointer_name;

                                                                              OR

data_type* pointer_name;

pointer_name  – the name of the pointer variable.
data_type  – decides the type of pointer variable.
*  indicates that the pointer_name is declared as pointer variable and will be used to store the address of another variable.
* Sign can be placed along with pointer name or data type. Both indicate the same.

Example

int *p;

In this example, int is the data type, and p is the name of the pointer variable.
Now p is used to store the address of any integer type variable.

Another Example

char *a, *b;
float *x, *y;

In this example, a and b are pointer variables of type char. They are used to store the addresses of char type variables.
And x and y are pointer variables of type float. They are used to store the addresses of float type variables.

Example of storing the address of a variable into the pointer of its type

Example no 1

C Programming Language Pointer to Integer

Demo

#include <stdio.h>
int main()
{
    int i = 5;
    int *p;
    p=&i; // address of integer variable i is assigned to pointer p
    printf("\n Address of i = %d",&i);
    printf("\n Value of pointer p = %d",p);
    printf("\n value of i = %d",i);
    printf("\n Value of i = %d",*p);
    printf("\n Address of p = %d",&p);
    return 0;
}

Output

Address of i = 2293324
Value of pointer p = 2293324
value of i = 5
Value of i = 5
Address of p = 2293312

Note

Address of variables may be different on every different machine because each machine stores values at different locations.

Example no 2

C Programming Language Pointer to Float

Demo

#include <stdio.h>
int main()
{
   float f = 7.5;
   float *p;
   p=&f;  // address of float variable f is assigned to pointer p
   printf("\n Address of f = %d",&f);
   printf("\n Value of pointer p = %d",p);
   printf("\n Value of f = %f",f);
   printf("\n Value of f = %f",*p);
   printf("\n Address of p = %d",&p);
   return 0;
}

Output

Address of f = 2293324
Value of pointer p = 2293324
Value of f = 7.500000
Value of f = 7.500000
Address of p = 2293312

There are two ways to initialize the pointer:

During Declaration

float f=7.5;
float *p=&f;  // pointer initialized during its declaration

After Declaration

float f=7.5;
float *p; // pointer declared
p=&f;     // pointer initialized after declaration

Why data type plays an important role while declaring a pointer variable?

You might doubt that although address of all type of variables to whom pointer variable points are of the same type & need equal space, then why it is necessary to use different types of data type for pointer variables, concerning the type of variable to which pointer variable points.

While declaring pointer variable data type plays an important role because depending on the data type of pointer variable, the compiler decides the number of bytes that should be retrieved from memory.

Example

#include <stdio.h>
int main()
{
    int x=5, *ptr1;
    char y='A', *ptr2;
    double z=24.35, *ptr3;
    ptr1=&x;
    ptr2=&y;
    ptr3=&z;
    printf("\n Space used by ptr1 = %d bytes",sizeof(ptr1));
    printf("\n Space used by ptr2 = %d bytes",sizeof(ptr2));
    printf("\n Space used by ptr3 = %d bytes",sizeof(ptr3));
    printf("\n");
    printf("\n Number of bytes retrieved by ptr1 = %d bytes",sizeof(*ptr1));
    printf("\n Number of bytes retrieved by ptr2 = %d bytes",sizeof(*ptr2));
    printf("\n Number of bytes retrieved by ptr3 = %d bytes",sizeof(*ptr3));
    return 0;
}

Output

Space used by ptr1 = 4 bytes
Space used by ptr2 = 4 bytes
Space used by ptr3 = 4 bytes

Number of bytes retrieved by ptr1 = 4 bytes
Number of bytes retrieved by ptr2 = 1 bytes
Number of bytes retrieved by ptr3 = 8 bytes

NULL pointer

A pointer that is assigned by NULL is known as a Null pointer.
The NULL pointer does not point to any memory address.
When we try to print the Null pointer, it gives zero because macro pre-processor uses zero to represent NULL.
It does not mean that we should use NULL instead of zero wherever we require variable to be initialized by zero.

Example of NULL pointer

#include<stdio.h>
int main()
{
    int *ptr=NULL;
    printf(" \n ptr = %d",ptr);
    return 0;
}

Output

ptr = 0

Note

We should never try to dereference NULL pointer. If we try to dereference NULL pointer, we get a runtime error.

 

Comment ( 1 )

  1. Atul Sampatrao Patil.
    April 12, 2018 at 5:52 am

    very good explanation on every point. Diagrams are helping to understand the concept in simple way. Both C and Java languages are explained in depth. I liked It.

    Sir please provide the same material for CPP.

Leave a reply