Chapter 8
Chapter 8
Introduction
A Pointer in C language is a variable which holds the address of another variable of same data
type.
Pointers are used to access memory and manipulate the address.
Pointers are one of the most distinct and exciting features of C language. It provides power and
flexibility to the language. Although pointers may appear a little confusing and complicated in
the beginning, but trust me, once you understand the concept, you will be able to do so much
more with C language.
Before we start understanding what pointers are and what they can do, let's start by
understanding what does "Address of a memory location" means?
Address in C : Whenever a variable is defined in C language, a memory location is assigned for
it, in which it's value will be stored. We can easily check this memory address, using
the & symbol.
If var is the name of the variable, then &var will give it's address.
Let's write a small program to see memory address of any variable that we define in our
program.
#include<stdio.h>
void main()
{
int var = 7;
printf("Value of the variable var is: %d\n", var);
printf("Memory address of the variable var is: %x\n", &var);
}
Value of the variable var is: 7
Memory address of the variable var is: bcc7a00
You must have also seen in the function scanf(), we mention &var to take user input for any
variable var.
scanf("%d", &var);
This is used to store the user inputted value to the address of the variable var.
Concept of Pointers : Whenever a variable is declared in a program, system allocates a location
i.e an address to that variable in the memory, to hold the assigned value. This location has its
own address number, which we just saw above. Let us assume that system has allocated memory
location 80F for a variable a.
int a = 10;
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;
}
p1 pointer variable can only hold the address of the variable a (i.e Number of indirection
operator(*)-1 variable). Similarly, p2 variable can only hold the address of variable p1. It
cannot hold the address of variable a.
p2 gives us the value at an address stored by the p2 pointer. p2 stores the address
of p1pointer and value at the address of p1is the address of variable a. Thus, *p2 prints
address of a.
**p2 can be read as *(*p2). Hence, it gives us the value stored at the address *p2. From
above statement, you know *p2 means the address of variable a. Hence, the value at the
address *p2 is 10. Thus, **p2 prints 10.
Here variable arr will give the base address, which is a constant pointer pointing to the first
element of the array, arr[0]. Hence arr contains the address of arr[0] i.e 1000. In short, arr has
two purpose - it is the name of the array and it acts as a pointer pointing towards the first element
in the array.
arr is equal to &arr[0] by default
We can also declare a pointer of type int to point to the array arr.
int *p;
p = arr;
// or,
p = &arr[0]; //both the statements are equivalent.
Now we can access every element of the array arr using p++ to move from one element to
another.
NOTE: You cannot decrement a pointer once incremented. p-- won't work.
Pointer to Array
As studied above, we can use a pointer to point to an array, and then we can use that pointer to
access the array elements. Lets have an example,
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i = 0; i < 5; i++)
{
printf("%d", *p);
p++;
}
return 0;
}
In the second approach memory wastage is more, hence it is prefered to use pointer in such
cases.
When we say memory wastage, it doesn't means that the strings will start occupying less space,
no, characters will take the same space, but when we define array of characters, a contiguos
memory space is located equal to the maximum size of the array, which is a wastage, which can
be avoided if we use pointers instead.
float* i;
i++;
In this case, size of pointer is still 2 bytes initially. But now, when we increment it, it will
increment by 4 bytes because float data type is of 4 bytes.
double* i;
i++;
Similarly, in this case, size of pointer is still 2 bytes. But now, when we increment it, it will
increment by 8 bytes because its data type is double.
Pointer to functions
It is possible to declare a pointer pointing to a function which can then be used as an argument in
another function. A pointer to a function is declared as follows,
type (*pointer-name)(parameter);
Here is an example :
int (*sum)(); //legal declaration of pointer to function
int *sum(); //This is not a declaration of pointer to function
A function pointer can point to a specific function when it is assigned the name of that function.
int sum(int, int);
Q. What are the advantages of using pointer in C programming? Write a program in C to find
second largest elements from an array containing N elements using concept of pointer.
Q. What is string? WAP to read a 3x3 square matrix, find minimum integer value of a matrix,
replace the diagonal elements by the minimum elements and display it using pointer.
Q. If Ptr is a pointer to user defined type or basic type, by how many bytes is Ptr is incremented
when the statement Ptr++ is executed?
Q. WAP that calls reverse array() to reverse the array and return the array and display the
element of reversed array using pointer.
Q. Illustrate with example that "Array is indirectly a pointer" . WAP to calculate sum and
average of integer numbers between M and N (where value of M and N are read from keyboard)
using pointer.
Q. A pointer variable is used to store address of some other variables, however, we need to
specify data type while declaring a pointer variable. Why?
Q. Briefly explain array of pointers. How are array and pointers related? Give example.
Q. Compare array and pointer.