0% found this document useful (0 votes)
3 views67 pages

Data_Structure_PPT_pointer2_

intro to pointer

Uploaded by

Awez Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views67 pages

Data_Structure_PPT_pointer2_

intro to pointer

Uploaded by

Awez Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Data Structure

CS2013

Dr. Shyamapada Mukherjee

Department of Computer Science and Engineering


NIT Rourkela-769008, India

August 5, 2025

1/61 Dr. Shyamapada Mukherjee Data Structure


Outline

1 Pointers

2 C Pointer

3 The Pointer Operators

4 Pointer to an Array or Array Pointer

5 Dynamic Memory Allocation in C

6 Pointer Arithmetic Operations

2/61 Dr. Shyamapada Mukherjee Data Structure


Pointers

Pointers

3/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

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.

4/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

C Pointer Example

1 # include < stdio .h >


2
3 int main () {
4
5 // Normal Variable
6 int var = 10;
7
8 // Pointer Variable ptr that
9 // stores address of var
10 int * ptr = & var ;
11
12 // Correct way to print an address
13 printf ( " % p \ n " , ( void *) ptr ) ;
14
15 return 0;
16 }

5/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

OUTPUT

Output: 0x7fffa0757dd4

6/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

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.

data type* name

Example

int *ptr;

7/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

Initialize the Pointer

Pointer initialization means assigning some address to the pointer variable.


In C, the () addressof operator is used to get the memory address of any variable.
This memory address is then stored in a pointer variable.

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.

8/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

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.

9/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

Example:1

1 # include < stdio .h >


2
3 int main () {
4 int * ptr1 ;
5 // Finding size using sizeof ()
6 printf ( " % zu \ n " , sizeof ( ptr1 ) ) ;
7
8 return 0;
9 }

Output
8

10/61 Dr. Shyamapada Mukherjee Data Structure


C Pointer

Example:2

1 # include < stdio .h >


2
3 int main () {
4 int * ptr1 ;
5 // Finding size using sizeof ()
6 printf ( " % zu \ n " , sizeof ( ptr1 ) ) ;
7
8 return 0;
9 }

Output
4

11/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

The Pointer Operators


Now you need to learn two new operators:

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

12/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

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.

In C, a variable is declared as a pointer by using the * (asterisk) operator. This operator *


is known as the indirection operator or the dereferencing operator.

Address and Indirection Operators:

inverse
& *

13/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

Special Types of Pointers

Pointer Types
NULL Pointer
Void Pointer
Wild Pointer
Dangling Pointer

14/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

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 }

16/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

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 }

17/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

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 }

18/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

Multilevel Pointers

In C, we can create two level pointers such as – **ptr2


In C, we can also create multi-level pointers with any number of levels such as – ***ptr3,
****ptr4, ******ptr5 and so on.
Most popular of them is double pointer (pointer to pointer). It stores the memory address
of another pointer.
Instead of pointing to a data value, they point to another pointer.

19/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

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 }

20/61 Dr. Shyamapada Mukherjee Data Structure


The Pointer Operators

Continue

var: 10

*ptr1: 10
**ptr2: 10

21/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

Pointer to an Array or Array Pointer

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.

22/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

Syntax of Array Pointer

An array pointer can be declared as shown:

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];

23/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

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

Access Array Using Array Pointer


1 # include < stdio .h >
2
3 int main () {
4 int arr [3] = { 5 , 10 , 15 };
5 int n = sizeof ( arr ) / sizeof ( arr [0]) ;
6 // Declare pointer variable
7 int (* ptr ) [3];
8 // Assign address of val [0] to ptr .
9 // We can use ptr =& val [0];( both are same )
10 ptr = & arr ;
11 for ( int i = 0; i < n ; i ++)
12 printf ( " % d " , (* ptr ) [ i ]) ;
13
14 return 0;
15 }
16
17 // Output : 5 10 15

24/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

Find the Size of Array Passed to a Function

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

25/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

Pointer to the First Element of Array vs Array Pointer


1 # include < stdio .h >
2
3 int main () {
4 int arr [5] = {1 , 2 , 3 , 4 , 5};
5 // Create a pointer to integer
6 int * p ; // Pointer to an array of 5 integers
7 int (* ptr ) [5]; // Points to 0 th element of the arr
8 p = arr ; // Points to the whole array arr
9 ptr = & arr ;
10 printf ( " p = % p \ n " , p ) ;
11 printf ( " * ptr = % p \ n \ n " , * ptr ) ;
12 p ++; // incrementing both pointers
13 ptr ++;
14 printf ( " p = % p \ n " , * p ) ;
15 printf ( " * ptr = % p " , ptr ) ;
16
17 return 0;
18 }

26/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

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.

27/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

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).

On dereferencing a pointer expression, we get a value pointed to by that pointer expression.

28/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

Pointer to Multidimensional Arrays

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];

29/61 Dr. Shyamapada Mukherjee Data Structure


Pointer to an Array or Array Pointer

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

31/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Dynamic Memory Allocation in C

Dynamic memory allocation in C is the process of managing memory during a program’s


execution (runtime) rather than at compile time.
This allows for greater flexibility in handling data structures and arrays whose sizes are
not known in advance or may change during the program’s lifecycle.

32/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Need of Dynamic Memory Allocation

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.

33/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Need of Dynamic Memory Allocation

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

Dynamic Memory Allocation in C

Dynamic memory allocation is possible in C by using 4 library functions provided by ¡stdlib.h¿


library:
Table of Content
malloc()
calloc()
free()
realloc()

35/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);

where size is the number of bytes to allocate.

36/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

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 }

37/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Output
12345

38/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

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.

39/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

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 }

40/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Output
00000

41/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

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);

where ptr is the pointer to the allocated memory.


After freeing a memory block, the pointer becomes invalid, and it is no longer pointing to a
valid memory location.

42/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Example

1 # include < stdio .h >


2 # include < stdlib .h >
3
4 int main () {
5 int * ptr = ( int *) calloc ( sizeof ( int ) , 5) ;
6
7 // Do some operations .....
8 for ( int i = 0; i < 5; i ++)
9 printf ( " % d " , ptr [ i ]) ;
10
11 // Free the memory after completing
12 // operations
13 free ( ptr ) ;
14
15 return 0;
16 }

43/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

Output
00000

44/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

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:

realloc(ptr, news izeq;

where, ptr is the pointer to the previously allocated memory block and
news izeisthereallocatedsizethatthememoryblockshouldhaveinbytes.

45/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

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 }

46/61 Dr. Shyamapada Mukherjee Data Structure


Dynamic Memory Allocation in C

47/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

Pointer Arithmetic Operations

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.

48/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

Increment Operation p``q

If a pointer is being incremented or decremented by one, the increment ++ and decrement --


operators can be used.

Increments the pointer to point to the


++vPtr; // or vPtr++;
next location in the array.
The increment operator can be applied to an operand of pointer type.

49/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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)

50/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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

Decrement Operation p´´q

The decrement operator can be applied to an operand of pointer type.


decrements the pointer to point to the
–vPtr; or vPtr–;
previous element of the array.

51/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

Decrement Operation (–)

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)

52/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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 .

53/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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

54/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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.

55/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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

56/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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];

57/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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.

58/61 Dr. Shyamapada Mukherjee Data Structure


Pointer Arithmetic Operations

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.

59/61 Dr. Shyamapada Mukherjee Data Structure


Multiple Choice Questions (MCQs)

1 What does a pointer store in C?


A. A variable
B. A value
C. An address
D. A data type
2 Which operator is used to get the address of a variable?
A. *
B. -¿
C. &
D. %
3 Which of the following pointer types is used to store any type of address?
A. NULL pointer
B. Void pointer
C. Wild pointer
D. Dangling pointer
59/61 Dr. Shyamapada Mukherjee Data Structure
Multiple Choice Questions (MCQs)

4 What is the size of a pointer on a 64-bit system?


A. 4 bytes
B. 8 bytes
C. 2 bytes
D. 1 byte
5 Which function releases dynamically allocated memory?
A. delete()
B. free()
C. remove()
D. dealloc()
6 What does the expression ptr++ do when ptr is an int*?
A. Increments the value
B. Points to the next integer
C. Increments the address by 1 byte
D. Decrements the pointer
59/61 Dr. Shyamapada Mukherjee Data Structure
Multiple Choice Questions (MCQs)

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)

10 Which of the following are valid pointer arithmetic operations?


A. Adding an integer to a pointer
B. Subtracting one pointer from another (same array)
C. Multiplying two pointers
D. Adding two pointers
11 Which situations can lead to a dangling pointer?
12 Which types of pointers are valid in C?
13 What are the uses of function pointers?
14 Which functions in C can allocate memory during runtime?
15 Which statements about pointer arithmetic are correct?
16 In C, how can you access the value stored in a pointer to pointer (double pointer)?
17 What issues can occur if a wild pointer is used?
18 Which types of pointers must be set to NULL after memory is freed?
60/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”

60/61 Dr. Shyamapada Mukherjee Data Structure


Multiple Choice Questions (MCQs)

Thank You

61/61 Dr. Shyamapada Mukherjee Data Structure

You might also like