INSTITUTE - UIE
Department of Engineering Foundations
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Logical Thinking & Problem Solving
Code:25CSH-107
Academic Session 2025-26
ODD Semester Jul-Dec 2025
Unit No. 3 Chapter No. 1 Lecture No.2
Topic : Hands on session on Flowcharts
Faculty Name-Dr. Sheenam
E_Code - E6717
Designation-Assistant Professor
Logical
Thinking &
Problem Solving
Course Objectives
2
The course aims to provide a strong foundation
in problem-solving using the C programming
language.
It enhances students' programming skills by
developing logical thinking and algorithmic
problem-solving abilities
By mastering C programming concepts, students
will be able to design and implement efficient
solutions for real-world problems
3
Course Outcomes
CO No Statement
CO1
To remember the concepts related to
fundamentals of C language, Algorithm/
pseudocode, Flowcharts and Problem solving
CO2
To understand the way of execution and debug
programs in C language.
CO3
To apply various constructs, loops, functions to
solve mathematical and scientific problem.
CO4
To analyse the dynamic behaviour of memory
by the use of pointers.
CO5
To design and develop modular programs for
real world problems using control structure and
selection structure.
Scheme of Evaluation
4
S. No.
Direct Evaluation
Instruments
Weightage of actual
conduct
Assessment tool
Frequency of
Task
Final Weightage
in Internal
Assessment
1
Unit-1 Practical
Evaluation
15 Marks Code-Blocks & Dev
C
3
45
2 Unit-2 Practical
Evaluation
15 Marks Hacker Rank
3
Unit-3 Practical
Evaluation / Mini
Project
15 Marks Code Fix problems
4
Lab MST 15 Marks for one
MST
Online GDB
compiler
1 per semester
15
5
External Exam 40 Marks Hacker Rank 1 per semester
40
6 Attendance NA NA NA NA
5
• 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.
• That is why they are also known as
double pointers.
• Syntax:
int **ptr; // declaring double pointers
Introduction
Source: https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
6
• 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.
7
Example:
#include <stdio.h>
// C program to demonstrate pointer to pointer
int main()
{
int var = 789;
// pointer for var
int *ptr2;
// double pointer for ptr2
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 = %dn", var );
printf("Value of var using single pointer = %dn", *ptr2 );
printf("Value of var using double pointer = %dn", **ptr1);
return 0;
}
Output:
Value of var = 789
Value of var using single pointer = 789
Value of var using double pointer = 789
8
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
9
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.
10
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");
for(i = 0; i< 5; i++)
{
printf("%d ",*(p+i));
}
}
Output:
printing array elements...
1 2 3 4 5
Note:
32-bit
For 32-bit int variable, it will be incremented by 2 bytes.
64-bit
For 64-bit int variable, it will be incremented by 4 bytes.
11
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.
12
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
13
C Pointer Addition
• We can add a value to the pointer variable.
• The formula of adding value to pointer is given below:
new_address= current_address + (number * size_of(data type))
• 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.
14
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.
15
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.
16
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.
17
• 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
18
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
Pointers and Arrays
• When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array.
• Base address i.e address of the first element of the array is also allocated by the compiler.
• Suppose we declare an array arr,
• int arr[5] = { 1, 2, 3, 4, 5 };
• Assuming that the base address of arr is 1000 and each integer requires two bytes, the five elements will be stored
as follows:
• 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
19
20
• 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 above program, the pointer *p will print all the values stored in the array one by one. We can also use the Base
address (a in above case) to act as a pointer and print all the values.
21
22
• The generalized form for using pointer with an array *(a+i) is same as a[i].
Source: https://www.studytonight.com/c/pointers-with-array.php
Pointer to Multidimensional Array
• A multidimensional array is of form, a[i][j].
• Lets see how we can make a pointer point to such an array.
• As we know now, name of the array gives its base address.
• In a[i][j], a will give the base address of this array, even a + 0 + 0 will also
give the base address, that is the address of a[0][0] element.
• Here is the generalized form for using pointer with multidimensional
arrays.
*(*(a + i) + j)
which is same as,
a[i][j]
23
Array of Pointers
• We can also have array of pointers.
• Pointers are very helpful in handling character array with rows of varying length.
• Example:
char *name[3] = {
"Adam",
"chris",
"Deniel"
};
//Now lets see same array without using pointer
char name[3][20] = {
"Adam",
"chris",
"Deniel"
};
24
Source: https://www.studytonight.com/c/pointers-with-array.php
25
• In the second approach memory wastage is more, hence it is prefered
to use pointer in such cases.
• Note: 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 contiguous
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.
• Example: Array of Pointers
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i;
for (i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %dn", i, var[i] );
}
return 0;
}
Output:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
26
27
Summary
Declaring Pointer to Pointer
is similar to declaring
pointer in C. The difference
is we have to place an
additional ‘*’ before the
name of pointer.
We can perform arithmetic
operations on the pointers
like addition, subtraction,
etc.
When an array is declared,
compiler allocates sufficient
amount of memory to
contain all the elements of
the array.
Base address i.e address of
the first element of the
array is also allocated by the
compiler.
You cannot decrement a
pointer once incremented.
p-- won't work
28
Frequently Asked question
Q1:Write a C program to Biggest value in the array using pointers in C
Solution:
#include <stdio.h>
int main()
{
long array[100], *maximum, size, c, location = 1;
printf("Enter the number of elements in arrayn");
scanf("%ld", &size);
printf("Enter %ld integersn", size);
for ( c = 0 ; c < size ; c++ )
scanf("%ld", &array[c]);
maximum = array;
*maximum = *array;
for (c = 1; c < size; c++)
{
if (*(array+c) > *maximum)
{
*maximum = *(array+c);
location = c+1;
}
}
printf("Maximum element is present at location
number %ld and it's value is %ld.n", location,
*maximum);
return 0;
}
29
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);
num = *ptr1 + *ptr2;
printf("Sum = %d", num);
return (0);
}
Output:
30
• Output:
31
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.
Q4) WHAT IS VOID POINTER IN C?
Ans:
3. Void pointer is a generic pointer that can be used to point another variable of
any data type.
4. Void pointer can store the address of variable belonging to any of the data type.
So, void pointer is also called as general purpose pointer.
32
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;
}
33
Discussion forum.
• C Program to read integers into an array and reversing them using
pointers
34
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
10. https://www.youtube.com/watch?v=kKKvGYAX_Zs
Class-wise feedback
35
THANK YOU

Chapter 7 Pointers Part 2 and its types.pptx

  • 1.
    INSTITUTE - UIE Departmentof Engineering Foundations Bachelor of Engineering (Computer Science & Engineering) Subject Name: Logical Thinking & Problem Solving Code:25CSH-107 Academic Session 2025-26 ODD Semester Jul-Dec 2025 Unit No. 3 Chapter No. 1 Lecture No.2 Topic : Hands on session on Flowcharts Faculty Name-Dr. Sheenam E_Code - E6717 Designation-Assistant Professor
  • 2.
    Logical Thinking & Problem Solving CourseObjectives 2 The course aims to provide a strong foundation in problem-solving using the C programming language. It enhances students' programming skills by developing logical thinking and algorithmic problem-solving abilities By mastering C programming concepts, students will be able to design and implement efficient solutions for real-world problems
  • 3.
    3 Course Outcomes CO NoStatement CO1 To remember the concepts related to fundamentals of C language, Algorithm/ pseudocode, Flowcharts and Problem solving CO2 To understand the way of execution and debug programs in C language. CO3 To apply various constructs, loops, functions to solve mathematical and scientific problem. CO4 To analyse the dynamic behaviour of memory by the use of pointers. CO5 To design and develop modular programs for real world problems using control structure and selection structure.
  • 4.
    Scheme of Evaluation 4 S.No. Direct Evaluation Instruments Weightage of actual conduct Assessment tool Frequency of Task Final Weightage in Internal Assessment 1 Unit-1 Practical Evaluation 15 Marks Code-Blocks & Dev C 3 45 2 Unit-2 Practical Evaluation 15 Marks Hacker Rank 3 Unit-3 Practical Evaluation / Mini Project 15 Marks Code Fix problems 4 Lab MST 15 Marks for one MST Online GDB compiler 1 per semester 15 5 External Exam 40 Marks Hacker Rank 1 per semester 40 6 Attendance NA NA NA NA
  • 5.
    5 • We alreadyknow 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. • That is why they are also known as double pointers. • Syntax: int **ptr; // declaring double pointers Introduction Source: https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/
  • 6.
    6 • Below diagramexplains 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.
  • 7.
    7 Example: #include <stdio.h> // Cprogram to demonstrate pointer to pointer int main() { int var = 789; // pointer for var int *ptr2; // double pointer for ptr2 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 = %dn", var ); printf("Value of var using single pointer = %dn", *ptr2 ); printf("Value of var using double pointer = %dn", **ptr1); return 0; } Output: Value of var = 789 Value of var using single pointer = 789 Value of var using double pointer = 789
  • 8.
    8 Pointer Arithmetic • Wecan 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
  • 9.
    9 Incrementing Pointer inC • 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.
  • 10.
    10 Example: Traversing anarray 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"); for(i = 0; i< 5; i++) { printf("%d ",*(p+i)); } } Output: printing array elements... 1 2 3 4 5 Note: 32-bit For 32-bit int variable, it will be incremented by 2 bytes. 64-bit For 64-bit int variable, it will be incremented by 4 bytes.
  • 11.
    11 Decrementing Pointer inC • 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.
  • 12.
    12 Example: Decrementing pointervariable 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
  • 13.
    13 C Pointer Addition •We can add a value to the pointer variable. • The formula of adding value to pointer is given below: new_address= current_address + (number * size_of(data type)) • 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.
  • 14.
    14 Example:Adding value topointer 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.
  • 15.
    15 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.
  • 16.
    16 Example: Subtracting valuefrom 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.
  • 17.
    17 • If twopointers 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
  • 18.
    18 Illegal arithmetic withpointers • 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
  • 19.
    Pointers and Arrays •When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. • Base address i.e address of the first element of the array is also allocated by the compiler. • Suppose we declare an array arr, • int arr[5] = { 1, 2, 3, 4, 5 }; • Assuming that the base address of arr is 1000 and each integer requires two bytes, the five elements will be stored as follows: • 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 19
  • 20.
    20 • We canalso 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.
  • 21.
    Pointer to Array Asstudied 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 above program, the pointer *p will print all the values stored in the array one by one. We can also use the Base address (a in above case) to act as a pointer and print all the values. 21
  • 22.
    22 • The generalizedform for using pointer with an array *(a+i) is same as a[i]. Source: https://www.studytonight.com/c/pointers-with-array.php
  • 23.
    Pointer to MultidimensionalArray • A multidimensional array is of form, a[i][j]. • Lets see how we can make a pointer point to such an array. • As we know now, name of the array gives its base address. • In a[i][j], a will give the base address of this array, even a + 0 + 0 will also give the base address, that is the address of a[0][0] element. • Here is the generalized form for using pointer with multidimensional arrays. *(*(a + i) + j) which is same as, a[i][j] 23
  • 24.
    Array of Pointers •We can also have array of pointers. • Pointers are very helpful in handling character array with rows of varying length. • Example: char *name[3] = { "Adam", "chris", "Deniel" }; //Now lets see same array without using pointer char name[3][20] = { "Adam", "chris", "Deniel" }; 24 Source: https://www.studytonight.com/c/pointers-with-array.php
  • 25.
    25 • In thesecond approach memory wastage is more, hence it is prefered to use pointer in such cases. • Note: 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 contiguous 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.
  • 26.
    • Example: Arrayof Pointers #include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { printf("Value of var[%d] = %dn", i, var[i] ); } return 0; } Output: Value of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200 26
  • 27.
    27 Summary Declaring Pointer toPointer is similar to declaring pointer in C. The difference is we have to place an additional ‘*’ before the name of pointer. We can perform arithmetic operations on the pointers like addition, subtraction, etc. When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. Base address i.e address of the first element of the array is also allocated by the compiler. You cannot decrement a pointer once incremented. p-- won't work
  • 28.
    28 Frequently Asked question Q1:Writea C program to Biggest value in the array using pointers in C Solution: #include <stdio.h> int main() { long array[100], *maximum, size, c, location = 1; printf("Enter the number of elements in arrayn"); scanf("%ld", &size); printf("Enter %ld integersn", size); for ( c = 0 ; c < size ; c++ ) scanf("%ld", &array[c]); maximum = array; *maximum = *array; for (c = 1; c < size; c++) { if (*(array+c) > *maximum) { *maximum = *(array+c); location = c+1; } } printf("Maximum element is present at location number %ld and it's value is %ld.n", location, *maximum); return 0; }
  • 29.
    29 2) Write CProgram 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); num = *ptr1 + *ptr2; printf("Sum = %d", num); return (0); } Output:
  • 30.
  • 31.
    31 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. Q4) WHAT IS VOID POINTER IN C? Ans: 3. Void pointer is a generic pointer that can be used to point another variable of any data type. 4. Void pointer can store the address of variable belonging to any of the data type. So, void pointer is also called as general purpose pointer.
  • 32.
    32 Assessment Questions: 1. CProgram 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; }
  • 33.
    33 Discussion forum. • CProgram to read integers into an array and reversing them using pointers
  • 34.
    34 REFERENCES Reference Books 1. Programmingin 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 10. https://www.youtube.com/watch?v=kKKvGYAX_Zs
  • 35.
  • 36.