Data_Structure_PPT_pointer2_
Data_Structure_PPT_pointer2_
CS2013
August 5, 2025
1 Pointers
2 C Pointer
Pointers
C Pointer
Pointer
A pointer is a variable that stores the memory address of another variable. Instead of
holding a direct value, it holds the address where the value is stored in memory.
It is the backbone of low-level memory manipulation in C.
By accessing the pointer directly we will only get the address which is stored in the
pointer.
C Pointer Example
OUTPUT
Output: 0x7fffa0757dd4
Declare a Pointer
A pointer is declared by specifying its name and type, just like simple variable declaration but
with an asterisk (*) symbol added before the pointer’s name.
Example
int *ptr;
Example
int var = 10;
// Initializing ptr
int *ptr = var;
Note
We can also declare and initialize the pointer in a single step. This is called pointer definition.
Size of Pointers
The size of a pointer in C depends on the architecture (bit system) of the machine, not the
data type it points to.
On a 32-bit system, all pointers typically occupy 4 bytes.
On a 64-bit system, all pointers typically occupy 8 bytes.
The size remains constant regardless of the data type (int*, char*, float*, etc.). We can verify
this using the sizeof operator.
Example:1
Output
8
Example:2
Output
4
Operator Description
& Address-of Operator
* Dereferencing Operator
The & operator is used to obtain the address of any required memory location. Therefore this
operator is also known as the address of operator.
2080
address of the location
temp 10
value stored
a memory location
continue
The value of &temp, in the example given above, is 2080. Thus the address of the memory
location with the name temp is 2080.
NOTE
It may be noted that we have been using the & operator all along, with scanf(). Thus,
during an input operation using scanf(), the required memory location is accessed using
its address.
inverse
& *
Pointer Types
NULL Pointer
Void Pointer
Wild Pointer
Dangling Pointer
NULL Pointer
The NULL Pointers are those pointers that do not point to any memory location.
They can be created by assigning NULL value to the pointer.
A pointer of any type can be assigned the NULL value.
1 # include < stdio .h >
2
3 int main () {
4 // Null pointer
5 int * ptr = NULL ;
6
7 return 0;
8 }
NULL pointers are generally used to represent the absence of any address.This allows us to
check whether the pointer is pointing to any valid memory location by checking if it is equal to
NULL.
15/61 Dr. Shyamapada Mukherjee Data Structure
The Pointer Operators
Void Pointer
The void pointers in C are the pointers of type void.
It means that they do not have any associated data type.
They are also called generic pointers.
example
1 # include < stdio .h >
2
3 int main () {
4 int x = 10;
5 void * ptr = & x ; // void pointer pointing to int
6
7 // To access value , cast it first
8 printf ( " Value = % d \ n " , *( int *) ptr ) ;
9
10 return 0;
11 }
Wild Pointers
The wild pointers are pointers that have not been initialized with something yet.
If values are updated using wild pointers, they could cause data abort or data corruption.
1 # include < stdio .h >
2
3 int main () {
4
5 // Wild Pointer
6 int * ptr ;
7
8 return 0;
9 }
Dangling Pointer
A pointer pointing to a memory location that has been deleted (or freed) is called a
dangling pointer.
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main () {
5 int * ptr = ( int *) malloc ( sizeof ( int ) ) ;
6
7 // After below free call , ptr becomes a dangling pointer
8 free ( ptr ) ;
9 printf ( " Memory freed \ n " ) ;
10
11 // removing Dangling Pointer
12 ptr = NULL ;
13
14 return 0;
15 }
Multilevel Pointers
Example
1 # include < stdio .h >
2
3 int main () {
4 int var = 10;
5
6 // Pointer to int
7 int * ptr1 = & var ;
8
9 // Pointer to pointer ( double pointer )
10 int ** ptr2 = & ptr1 ;
11
12 // Accessing values using all three
13 printf ( " var : % d \ n " , var ) ;
14 printf ( " * ptr1 : % d \ n " , * ptr1 ) ;
15 printf ( " ** ptr2 : % d " , ** ptr2 ) ;
16
17 return 0;
18 }
Continue
var: 10
*ptr1: 10
**ptr2: 10
A pointer to an array is a pointer that points to the whole array instead of the first
element of the array.
It considers the whole array as a single unit instead of it being a collection of given
elements.
type(*ptr)[size];
where,
type: Type of data that the array holds.
ptr: Name of the pointer variable.
size: Size of the array to which the pointer will point.
For example,
int (*ptr)[10];
Example
1 # include < stdio .h >
2
3 int main () {
4 int arr [5] = { 1 , 2 , 3 , 4 , 5 };
5 int * ptr = arr ;
6
7 printf ( " % p \ n " , ptr ) ;
8 return 0;
9 }
Output
0x16b852cf0
24/61 Dr. Shyamapada Mukherjee Data Structure
Pointer to an Array or Array Pointer
Normally, it is impossible to find the size of array inside a function, but if we pass the pointer
to an array, the it is possible.
1 # include < stdio .h >
2 // Function that takes array as argument
3 void foo ( int (* arr ) [5]) {
4 printf ( " % lu " , sizeof (* arr ) ) ;
5 }
6 int main () {
7 int arr [5];
8 // Passing the address of arr
9 foo (& arr ) ;
10 return 0;
11 }
Output : 20
Continue
p = 0x7fff81e4caf0
ptr = 0x7fff81e4caf0
p = 0x7fff81e4caf4
ptr = 0x7fff81e4cb04
Here, p is pointer to 0th element of the array arr, while ptr is a pointer that points to the
whole array arr.
The base type of p is int while base type of ptr is ’an array of 5 integers’.
We know that the pointer arithmetic is performed relative to the base size, so if we write
ptr++, then the pointer ptr will be shifted forward by 20 bytes.
Continue
The following figure shows the pointer p and ptr. The darker arrow denotes a pointer to an
array. (address may vary in each execution of the program).
1. Pointers to 2D Arrays
To define a pointer to a 2D array, both the number of rows and columns of the array must be
specified in the pointer declaration.
type *(ptr)[row][cols];
2. Pointers to 3D Arrays
To define a pointer to a 2D array, both the number of rows and columns of the array must be
specified in the pointer declaration.
type *(ptr)[depth][row][cols];
Example
1 # include < stdio .h >
2
3 int main () {
4 int arr [2][3][2] = {{{1 , 2} , {3 , 4} , {5 , 6}} ,
5 {{7 , 8} , {9 , 10} , {11 , 12}}};
6 // Pointer to the 3 D array
7 int (* ptr ) [2][3][2] = & arr ;
8 // Traversing the 3 D array using the pointer
9 for ( int i = 0; i < 2; i ++) {
10 for ( int j = 0; j < 3; j ++) {
11 for ( int k = 0; k < 2; k ++) {
12 printf ( " % d " , (* ptr ) [ i ][ j ][ k ]) ;
13 }
14 printf ( " \ n " ) ;
15 }
16 printf ( " \ n " ) ;
17 }
18 return 0;
19 }
30/61 Dr. Shyamapada Mukherjee Data Structure
Pointer to an Array or Array Pointer
Continue
12
34
56
78
9 10
11 12
we know that size of an array in C is fixed and should be known at compile time. There can be
two problems:
1. The size of the array is not sufficient to store all the elements.
2. This size of the array is much more than what is required to store the elements. This leads
to the wastage of memory.
This is where the dynamic memory allocation comes in. The size of the array can be increased
if more elements are to be inserted and decreased of less elements are inserted.
34/61 Dr. Shyamapada Mukherjee Data Structure
Dynamic Memory Allocation in C
malloc()
The malloc() (stands for memory allocation) function is used to allocate a single block of
contiguous memory on the heap at runtime.
The memory allocated by malloc() is uninitialized, meaning it contains garbage values.
Syntax
malloc(size);
Example
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main () {
5 int * ptr = ( int *) malloc ( sizeof ( int ) * 5) ;
6 // Checking if failed or pass
7 if ( ptr == NULL ) {
8 printf ( " Allocation Failed " ) ;
9 exit (0) ;
10 }
11 // Populate the array
12 for ( int i = 0; i < 5; i ++)
13 ptr [ i ] = i + 1;
14 // Print the array
15 for ( int i = 0; i < 5; i ++)
16 printf ( " % d " , ptr [ i ]) ;
17 return 0;
18 }
Output
12345
calloc()
The calloc() (stands for contiguous allocation) function is similar to malloc(), but it initializes
the allocated memory to zero. It is used when you need memory with default zero values.
Syntax:
calloc(n, size);
where n is the number of elements and size is the size of each element in bytes.
Example
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main () {
5 int * ptr = ( int *) calloc (5 , sizeof ( int ) ) ;
6
7 // Checking if failed or pass
8 if ( ptr == NULL ) {
9 printf ( " Allocation Failed " ) ;
10 exit (0) ;
11 }
12 // No need to populate as already
13 // initialized to 0
14 // Print the array
15 for ( int i = 0; i < 5; i ++)
16 printf ( " % d " , ptr [ i +1]) ;
17 return 0;
18 }
Output
00000
free()
The memory allocated using functions malloc() and calloc() is not de-allocated on their own.
The free() function is used to release dynamically allocated memory back to the operating
system. It is essential to free memory that is no longer needed to avoid memory leaks.
Syntax:
free(ptr);
Example
Output
00000
realloc()
realloc() function is used to resize a previously allocated memory block. It allows you to
change the size of an existing memory allocation without needing to free the old memory and
allocate a new block.
Syntax:
where, ptr is the pointer to the previously allocated memory block and
news izeisthereallocatedsizethatthememoryblockshouldhaveinbytes.
Example
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main () {
5 int * ptr = ( int *) malloc (5 * sizeof ( int ) ) ;
6
7 // Resize the memory block to hold 10 integers
8 ptr = ( int *) realloc ( ptr , 10 * sizeof ( int ) ) ;
9
10 // Check for allocation failure
11 if ( ptr == NULL ) {
12 printf ( " Memory Reallocation Failed " ) ;
13 exit (0) ;
14 }
15
16 return 0;
17 }
Pointer arithmetic refers to the application of some arithmetic operations on pointers. Pointers
are valid operands in arithmetic expressions, assignment expressions and comparison
expressions. However, not all the operators normally used in these expressions, are also valid in
combination with pointer variables.
The operators that can be used in pointer arithmetic are:
Increment p``q,
Decrement p´´q,
an integer may be added to a pointer p` or ` “q,
an integer may be subtracted from a pointer p´ or ´ “q and
One pointer may be subtracted from another, this last operation is meaningful only when
both pointers point to elements of the same array.
Continue
Operator Type of Resultant Initial Val- Example Final Values How to decide?
operand type ues
Increment Pointer to Pointer to
(++) type Ñ type Ñ
Ex- Post- float* float* ptr = 2000, str = ptr++ ptr = 2004,
Output = Initial value of
1 increment str = ? str = 2000
pointer
Ex- Pre- float* float* ptr = 2000,
str = ++ptr ptr = 2004,
Output = Initial value of
2 increment str = ? str = 2004
pointer + sizeof (the
pointer type)
Note: In both the increment operations (Ex-1 & Ex-2): Value of pointer = value of ptr +
sizeof (the pointer type)
Program:
#include <stdio.h>
int main()
{
int *ptr, i;
ptr = &i;
printf("Address = %p\n", ptr);
ptr++;
printf("Address = %p\n", ptr);
return 0;
}
Explanation: In fact, this program displays two addresses and the second one is four bytes
higher than the first one. The statement ptr ` ` increases its value by four because it is
declared as a pointer to int.
51/61 Dr. Shyamapada Mukherjee Data Structure
Pointer Arithmetic Operations
Operator Type of Resultant Initial Val- Example Final Values How to decide?
operand type ues
Decrement Pointer to Pointer to
(- -) type Ñ type Ñ
Ex- Post- float* float* ptr = 1000 str = ptr- - ptr = 996
Output = Initial value of
1 decrement str = ? str = 1000
pointer
Ex- Pre- float* float* ptr = 1000
str = - -ptr ptr = 996
Output = Initial value
2 decrement str = ? str = 996
of pointer - sizeof (the
pointer type)
Note: In both the decrement operations (Ex-1 & Ex-2): Value of pointer = value of ptr -
sizeof (the pointer type)
Addition Operation p` or ` “q
1 An expression of integer type can be added to an expression of pointer type. The result of
such operation would have the same type as that of pointer type operand. If ptr is a
pointer to an object, then ’adding 1 to pointer’ (i.e., ptr ` 1) points to the next object.
Similarly, ptr ` i would point to the i th object beyond the one the ptr currently points to.
2 Addition of two pointers is not allowed.
3 The addition of a pointer and an integer is commutative, i.e., ptr ` 1 is same as 1 ` ptr .
Continue
Operator Type of Type of Resultant Initial Val- Example Final Val- How to decide?
operand-1 operand-2 type ues ues
Addition Pointer to — Pointer to — — — Output = Initial value of
(+) type Ñ type Ñ pointer + integer operand ˆ
sizeof (the pointer type)
Ex-1 float* int float* ptr = 1000 ptr = ptr + ptr = 1004 1000 + (1 ˆ 4) = 1004
1 Here we assume
sizeof(float) = 4
Ex-2 int* int int* ptr = 1000 ptr = ptr + ptr = 1006 1000 + (3 ˆ 2) = 1006
3 Here we assume sizeof(int)
=2
Ex-3 pointer pointer — — — — Addition of two pointers
is not allowed
Subtraction Operation p´ or ´ “q
1 A pointer and an integer can be subtracted.
2 Subtraction of integer and pointer is not commutative, i.e., ptr ´ 1 is not the same as
1 ´ ptr . The operation 1 ´ ptr is illegal.
3 Two pointers can also be subtracted. Pointer subtraction is meaningful only if both
the pointers point to the elements of the same array. The result of the operation is the
difference in subscripts of two array elements.
Continue
Operator Type of Type of Resultant Initial Val- Example Final Val- How to decide?
operand-1 operand-2 type ues ues
Subtraction Pointer to Integer Pointer to Output = Initial value of
p´q type Ñ type Ñ pointer - integer operand ˆ
sizeof(the pointer type)
Ex-1 float* int float* ptr = 1000 ptr = ptr - 1 ptr = 996 1000 ´ p1 ˆ 4q “ 996
6 Here we assume
sizeof(float)=4
Ex-2 int* int int* ptr = 1000 ptr = ptr - 3 ptr = 994 1000 ´ p3 ˆ 2q “ 994
6 Here we assume
sizeof(float)=4
Ex-3 Pointer to Pointer to Integer type p1 “ 1000 output output “ 2 Output = (operand-2 -
type Ñ type Ñ int p2 “ 1008 “ p2 ´ p1 operand-1)/ sizeof(the
float* float* pointer type)
(1008 - 1000)/sizeof(float)
= 8/4 = 2
Example:
int v[5]; is an array, its first element is at location 3000 in memory.
int * vPtr;
Assume pointer vPtr has been initialized to point to v[0] - i.e., the value of vPtr is 3000. So,
vPtr = v;
OR
vPtr = v[0];
Subtraction or Differencing
Pointer variables may be subtracted from one another. For example, if vPtr contains the
location 3000, and v2Ptr contains the address 3008, the statement,
x = v2Ptr - vPtr;
Would assign to x the number of array elements from vPtr to v2Ptr, in this case 2 (not 8),
meaning that these pointers point to entities separated by two ints, not by 2 bytes.
For Example
Suppose that the vPtr and v2Ptr are pointers to two integer variables stored in the
addresses 1000 and 1040, respectively. The result of (ptr2-ptr1) is not equal to 40 (i.
e., 1040-1000), but equal to (1040-1000)/sizeof(int) = 40/4 = 10. This number
indicates the number of integers between the two pointers.
If the type of both pointers and variables was char, the result of (ptr2-ptr1) would be 40
because the size of the char type is one byte.
Observation:
Pointer arithmetic is undefined unless performed on an array. We cannot assume that
two variables of the same type are stored contiguously in memory unless they’re adjacent
elements of an array.
7 Which operator is used to access the value at the address a pointer is pointing to?
A. #
B. &
C. *
D. $
8 Which of the following are types of special pointers in C?
A. NULL Pointer
B. Wild Pointer
C. Function Pointer
D. Dynamic Pointer
9 Which of the following functions are used for dynamic memory allocation in C?
A. malloc()
B. calloc()
C. new()
D. realloc()
59/61 Dr. Shyamapada Mukherjee Data Structure
Multiple Choice Questions (MCQs)
References
Karumanchi, N. Data Structures and Algorithms Made Easy: Data Structure and
Algorithmic Puzzles, 5thed., CareerMonk Publications, 2016. ISBN978-8193245279
https://archive.org/details/data-structures-and-algorithms-narasimha-karumanchi
Schaum’s Outline of Data Structures, revised edition, SeymourLipschutz. New Delhi:
McGraw-Hill Education (India) Pvt Ltd, 2014. ISBN 978-1259029967
https://gnindia.dronacharya.info/IT/3rdSem/Downloads/DataStructure/Books/DATA-
STRUCTURE-BOOK-3.pdf
Ghosh Partha, ”Basic Concepts of C Programming”
Thank You