Lecture 3.1.2 Pointer To Pointer, Pointer Arithmetic
Lecture 3.1.2 Pointer To Pointer, Pointer Arithmetic
Course Objectives
CO5 Design and develop modular programs for real world problems us-
ing control structure and selection structure.
3
ASSESSMENT PATTERN
The performance of students is evaluated as follows:
Theory Practical
Continuous Internal As- Semester End Examina- Continuous Internal Semester End Exam-
Components sessment (CAE) tion (SEE) Assessment (CAE) ination (SEE)
Marks 40 60 60 40
Total Marks 100 100
4
Introduction
• We already know that a pointer points to
a location in memory and thus used to
store the address of variables.
• So, when we define a pointer to pointer.
• The first pointer is used to store the
address of the variable.
• And the second pointer is used to store
the address of the first pointer. Source: https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
• That is why they are also known as
double pointers.
• Syntax:
int **ptr; // declaring double pointers
5
• Below diagram explains the concept of Double Pointers:
Source: https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
• The above diagram shows the memory representation of a pointer to pointer. The first pointer ptr1 stores
the address of the variable and the second pointer ptr2 stores the address of the first pointer.
6
Example:
#include <stdio.h>
// C program to demonstrate pointer to pointer
int main()
{
int var = 789;
Output:
// pointer for var
Value of var = 789
int *ptr2;
Value of var using single pointer = 789
// double pointer for ptr2
Value of var using double pointer = 789
int **ptr1;
// storing address of var in ptr2
ptr2 = &var;
// Storing address of ptr2 in ptr1
ptr1 = &ptr2;
// Displaying value of var using both single and double pointers
printf("Value of var = %d\n", var );
printf("Value of var using single pointer = %d\n", *ptr2 );
printf("Value of var using double pointer = %d\n", **ptr1);
return 0;
}
7
Pointer Arithmetic
• We can perform arithmetic operations on the pointers like addition, subtraction,
etc.
• However, as we know that pointer contains the address, the result of an
arithmetic operation performed on the pointer will also be a pointer if the other
operand is of type integer.
• In pointer-from-pointer subtraction, the result will be an integer value.
• Following arithmetic operations are possible on the pointer in C language:
1. Increment
2. Decrement
3. Addition
4. Subtraction
5. Comparison
8
Incrementing Pointer in C
• If we increment a pointer by 1, the pointer will start pointing to the
immediate next location.
• This is somewhat different from the general arithmetic since the value of
the pointer will get increased by the size of the data type to which the
pointer is pointing.
• We can traverse an array by using the increment operation on a pointer
which will keep pointing to every element of the array, perform some
operation on that, and update itself in a loop.
• The Rule to increment the pointer is given below:
new_address= current_address + i * size_of(data type)
where i is the number by which the pointer get increased.
9
Example: Traversing an array by using pointer
#include<stdio.h>
void main ()
{
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
int i;
printf("printing array elements...\n");
Note:
for(i = 0; i< 5; i++)
32-bit
{ For 32-bit int variable, it will be incremented by 2 bytes.
printf("%d ",*(p+i));
} 64-bit
For 64-bit int variable, it will be incremented by 4 bytes.
}
Output:
printing array elements...
1 2 3 4 5
10
Decrementing Pointer in C
• Like increment, we can decrement a pointer variable.
• If we decrement a pointer, it will start pointing to the previous
location.
• The formula of decrementing the pointer is given below:
new_address= current_address - i * size_of(data type)
• Note:
32-bit: For 32-bit int variable, it will be decremented by 2 bytes.
64-bit: For 64-bit int variable, it will be decremented by 4 bytes.
11
Example: Decrementing pointer variable on 64-bit OS.
#include <stdio.h>
void main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-1;
printf("After decrement: Address of p variable is %u \n",p); // P will now point
to the immediate previous location.
}
Output:
Address of p variable is 3214864300
After decrement: Address of p variable is 3214864296
12
C Pointer Addition
• We can add a value to the pointer variable.
• Note:
32-bit: For 32-bit int variable, it will add 2 * number.
64-bit: For 64-bit int variable, it will add 4 * number.
13
Example: Adding value to pointer variable on 64-bit architecture.
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u \n",p);
return 0;
}
Output:
Address of p variable is 3214864300
After adding 3: Address of p variable is 3214864312
• As you can see, the address of p is 3214864300. But after adding 3 with p variable, it is
3214864312, i.e., 4*3=12 increment. Since we are using 64-bit architecture, it increments 12.
But if we were using 32-bit architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer
value occupies 2-byte memory in 32-bit OS.
14
C Pointer Subtraction
• Like pointer addition, we can subtract a value from the pointer variable.
Subtracting any number from a pointer will give an address.
• The formula of subtracting value from the pointer variable is given below:
new_address= current_address - (number * size_of(data type))
• Note:
32-bit:For 32-bit int variable, it will subtract 2 * number.
64-bit:For 64-bit int variable, it will subtract 4 * number.
15
Example: Subtracting value from the pointer variable on 64-bit architecture.
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u \n",p);
return 0;
}
Output:
Address of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
• You can see after subtracting 3 from the pointer variable, it is 12 (4*3) less than the previous address value.
• However, instead of subtracting a number, we can also subtract an address from another address (pointer).
• This will result in a number.
• It will not be a simple arithmetic operation, but it will follow the following rule.
16
• If two pointers are of the same type,
Address2 - Address1 = (Subtraction of two addresses)/size of data type which pointer
points
• Consider the following example to subtract one pointer from an another.
#include<stdio.h>
void main ()
{
int i = 100;
int *p = &i;
int *temp;
temp = p;
p = p + 3;
printf("Pointer Subtraction: %d - %d = %d",p, temp, p-temp);
}
Output:
Pointer Subtraction: 1030585080 - 1030585068 = 3
17
Illegal arithmetic with pointers
• There are various operations which can not be performed on pointers.
• Since, pointer stores address hence we must ignore the operations which may
lead to an illegal address, for example, addition, and multiplication.
• A list of such operations is given below.
Address + Address = illegal
Address * Address = illegal
Address % Address = illegal
Address / Address = illegal
Address & Address = illegal
Address ^ Address = illegal
Address | Address = illegal
~Address = illegal
18
Summary
19
Frequently Asked question
Q1:Write a C program to Biggest value in the array using pointers in C
Solution:
#include <stdio.h> for (c = 1; c < size; c++)
int main() {
{ if (*(array+c) > *maximum)
long array[100], *maximum, size, c, location = 1; {
printf("Enter the number of elements in array\n"); *maximum = *(array+c);
scanf("%ld", &size); location = c+1;
printf("Enter %ld integers\n", size); }
for ( c = 0 ; c < size ; c++ ) }
scanf("%ld", &array[c]); printf("Maximum element is present at location
maximum = array; number %ld and it's value is %ld.\n", location,
*maximum = *array; *maximum);
return 0;
}
20
2) Write C Program to Add Two Numbers Using Pointer
Solution:
#include<stdio.h>
int main() {
int *ptr1, *ptr2;
int num;
printf("\nEnter two numbers : ");
scanf("%d %d", ptr1, ptr2); Output:
num = *ptr1 + *ptr2;
printf("Sum = %d", num);
return (0);
}
21
• Output:
22
Q3) What is “&” and “*” operators in C?
Ans:
1. “*” Operator is used as pointer to a variable. Example: * a where * is
pointer to the variable a.
2. & operator is used to get the address of the variable. Example: &a will give
address of a.
23
Assessment Questions:
1. C Program to compute sum of the array elements using pointers ?
2. Write a C program to Find transpose of a matrix.
3. Write a C program to find the length of string using pointers.
4. What will be the output of the C program?
#include<stdio.h>
#include<string.h>
int main(){
char *ptr = "hello";
char a[22];
*ptr = "world";
printf("\n%s %s",ptr, a);
return 0;
}
5. What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
k++;
printf("%d ",**k);
return 0;
}
24
Discussion forum.
• C Program to read integers into an array and reversing them using
pointers
25
REFERENCES
Reference Books
1. Programming in C by Reema Thareja.
2. Programming in ANSI C by E. Balaguruswamy, Tata McGraw Hill.
3. Programming with C (Schaum's Outline Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
4. The C Programming Language by Brian W. Kernighan, Dennis Ritchie, Pearson
education.
Websites:
5. https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
6. https://www.javatpoint.com/pointer-arithmetic-in-c#:~:text=%E2%86%92%20%E2%86%90
%20prev-,Pointer%20Arithmetic%20in%20C,operand%20is%20of%20type%20integer.&text
=Subtraction,-Comparison
7. https://www.studytonight.com/c/pointers-with-array.php
YouTube Links:
8. https://www.youtube.com/watch?v=ahKfY1EsWd8&t=1s
9. https://www.youtube.com/watch?v=jYuqC9UD4GI
26
10. https://www.youtube.com/watch?v=kKKvGYAX_Zs
THANK YOU