0% found this document useful (0 votes)
35K views43 pages

C Lec106-110

Pointer arithmetic allows pointers to be manipulated using addition and subtraction operations. Adding an integer to a pointer increases the address it points to by the integer multiplied by the size of the data type pointed to. Subtracting from a pointer decreases the address. For example, if p points to memory location 1000, then p+3 would point to location 1000 + 3 * size_of_data_type. This allows pointers to easily traverse through arrays and other contiguous memory blocks.

Uploaded by

Karan Sahu
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)
35K views43 pages

C Lec106-110

Pointer arithmetic allows pointers to be manipulated using addition and subtraction operations. Adding an integer to a pointer increases the address it points to by the integer multiplied by the size of the data type pointed to. Subtracting from a pointer decreases the address. For example, if p points to memory location 1000, then p+3 would point to location 1000 + 3 * size_of_data_type. This allows pointers to easily traverse through arrays and other contiguous memory blocks.

Uploaded by

Karan Sahu
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/ 43

C

PROGRAMMING AND
DATA STRUCTURES
Application of pointers: Largest and
Smallest Element in an Array
NESO ACADEMY
IDEA
min Max

int a[] = {23, 45, 6, 98};


6 98
int min, max; min = max = 23
min = max = a[o];
for i = 1 to 3: 2)
(2) 45 > 23

max = 45
(1)if a[i] < min then
min = a[i] 6< 23

(2)if arr[i] > max then min =6


max = a[i] (2) 98 > 45
max = 98
NESO ACADEMY
Start here x minMax.c x
1/Program to find the largest and smallest element in an array

#include <stdio.h>

void minMax (int arr(], int len, int *min, int *max)
6
7
8
*min = *max = arr [0]
int ij
:
for (i-1l; i<len; it+)
10
11 if (arr [i] > *max)
12
13
*max =
arr [i];
if (arr [i] < *min)
14
15
*min = arr (i] ;
16
17
18 pint main () {

19 int a[] -
(23, 4, 21, 98, 987, 45, 32, 10, 123, 986, 50, 3, 4,5):
20 int min, max;
21
22
int m
minMà
sizeof(a)/sizeof (a[0]):
a, len, &min, &max)
23
24
printf ("Minimum value in the array is: %d and Maximum value is: %d", min, max)
return 0;
:
25
26
C

PROGRAMMING AND
DATA STRUCTURES
Returning pointers:
Find the mid of the array
NESO ACADEMY
int main()

int a[] = {1, 2, 3, 4, 5};


int n = sizeof(a) /sizeof (a[o]);
int *mid = findMid (a, n) ;
printf("%d", *mid) ;
return 0;

}
int *findMid (int a[], int n)
{
return &a[n/2];
OUTPUT: 3

NESO ACADEMY
WORD OF CAUTION

Never ever try to return the address of an autonmatic variable

For example:

int *fun() Warning: function returns address


{ of local variable
int i=10;
return &i;
}
int main() {
int *p = fun();
printf("%d", *p);
}
NESO ACADEMY
C

PROGRAMMING AND
DATASTRUCTURES
Important questions

NESOACADEMY
Question1t: Consider the following two statements
int *p = &i;
p = &i;

statement is the declaration and second is simple assignment statement.


First
Why isn't in second statement, p is preceded by * symbol?

Solution: InC,* symbol has differentmeanings depending on the context in which


it's used.

Atthe timeof declaration, *Symbol is not acting as an indirection operator.

* symbol in the first statement tells thecompiler that p is a pointer to an integer.


But, if wewrite *p=&i then it is wrong, because here * symbol indicates the
indirection operator and we cannot assign the address to some integer variable.

Therefore, in the second statement,there is no need of *symbol in front of p. It

simply means we are assigning the address to a pointer.


NESO ACADEMY
Question 2: What is the output of the following program

void fun(const int *p)


{
*p = 0;
} Output:

int main() { Error: Assignment of read-only location *p


const int i = 10;
fun(&i);
return 0;

NESO ACADEMY
Question 3: How to print the address of a variable?

Solution: use %p asa format specifier in printffunction

int main() {
int 10;
i =
int *p = &i;
printf("The address of variable i is %p", p);

}
return ;
iOutput: The address of variable i is 0x7ffd5b9a987 c

i
NESO ACADEMY
Question 4: If is
variable and p points
i a to i, which of the
following expressions are aliases of i?

a) *p b) *&p c) &p d) *i e) *&i

Solution: Options (a)and (e) are the aliases of variatble i

Example a) *p = *(1000) = 10
int i = 10; b) *&p = *(&p) = *(2000) = 1000
int *p =
&i;
C) &p = 2000
i d) *i= *(10) doesn't make sense

10 1000 e) *&i = *(&i) = *(1000) = 10

1000 2000

NESO ACADEMY
12:35 AMO
C

PROGRAMMING AND
DATA|STRUCTURES
Pointer Arithmetic -Addition
NESO ACADEMY
P

1 2 3 4 5 6 7

p = &a[e]

NESO ACADEMY
WHAT HAPPENS IF WE ADD SOME INTEGER TO THE POINTER?

1 2 3 4 5 6 7

means moving the pointer 3 positions in forward


P= p +3 direction.

NESO ACADEMY
WHAT HAPPENS IF WE ADD SOME INTEGER TO THE POINTER?

1 2 3 4 5 6 7

3 positions
P=p + 3 means moving the pointer
direction.
in forward

NESO ACADEMY
IN OTHER WORDS..

1 2 3 4 5 6 7

Initially, if p points to a[i], then

NESO ACADEMY
ip =p+j
=p + ji = ga[i + jli
IN OTHER WORDS..

1 2 4 5 6 7

Initially, if p points to a[o], then

ip =p + 3! E &a[0 + 3]
NESO ACADEMY
IN OTHER WORDS..

1 2 4 5 6 7

Initially, if p points to a[o], then

iP =p +3 &a[3]

NESO ACADEMY
AcTUALLY WHAT HAPPENED?

1 2 3 4 5 6 7
1000 1004 1008 1012 1016 1020 1024 1028

ip = &a[O] ip = 1000

NESO ACADEMY
ACTUALLY WHAT HAPPENED?

1 2 3 4 5 6 7
1000 1004 1008 1012 1016 1020 1024 1028

p =p +1 Ep = 1000 + 1 x 4

NESO ACADEMY
AcTUALLY WHAT HAPPENED?

1 2 3 4 5 6 7
1000 1004 1008 1012 1016 1020 1024 1028

p =p +1 p=1004
NESO ACADEMY
ACTUALLY WHAT HAPPENED?

1 2 3 4 5 6 7
1000 1004 1008 1012 1016 1020 1024 1028

iP =p +3 E p = 1000 + 3x 4
NESO ACADEMY
ACTUALLY WHAT HAPPENED?

1 2 3 4 5 6 7
1000 1004 1008 1012 1016 1020 1024 1028

ip =p +3 p=1012
NESO ACADEMY
C

PROGRAMMING AND
DATA|STRUCTURES
Pointer Arithmetic -Subtraction
NESO ACADEMY
RECALL-ADDING INTEGER TO A POINTER

1 2 3 45 6 7

ip =p + 3

NESO ACADEMY
RECALL-ADDING INTEGER TO A POINTER

1 2 3 4 5 6 7

+3 means moving the pointer 3 positions in forward


ip = p direction.

NESO ACADEMY
INOTHER WORDS...

1 2 3 4 5 6 7

Initially, if p points to a[i], then

p =p - j = &[i - j]
NESO ACADEMY
IN OTHER WORDS..

1 2 3 4 5 6 7

Initially, if p points to a[3], then

ip = p - 3= &a[3 - 3]

NESO ACADEMY
INOTHER WORDS...

1 2 3 4 56 7

Initially, if p points to a[3], then

p =p-- 33 =
E&a[e]
NESO ACADEMY
IN REALITY...

1 2 3 4 5 6 7

ip= p - 3 E ip = 1012 - 3 x 4
4

NESO ACADEMY
IN REALITY...

1 2 3 4 5 6 7

iP = p- 3 p = 1000
NESO ACADEMY
SUBTRACTING ONE POINTER FROM ANOTHER

NESO ACADEMY
SUBTRACTING ONE POINTER FROM ANOTHER POINTER

Result is distancebetween pointers

1 2 3 4 5 6 7

q- p = 4

NESO ACADEMY
SUBTRACTING ONE POINTER FROM ANOTHER POINTER

Result isdistancebetween pointers

1 2 3 4 5 6 7

IP - q = -4
NESO ACADEMY
IN REALITY...

1 2 3 4 5 6 7
Ifbase address of array is1000

p= 1008 q= 1024

NESO ACADEMY
IN REALITY...

1 2 3 4 5 6 7
f base address of array is 1000

p=1008 q= 1024
NESO ACADEMY
IN REALITY...

1 2 3 4 5 6 7
fbase address of array is 1000

9- p = 16

NESO ACADEMY
IN REALITY...

I 1 2 3 4 5 6 7
base address of array is 1000
If

p= 1008 i9= p + 4 q= 1008 + 4x4

NESO ACADEMY
IN REALITY..

I 1 2 3 4 5 6 7
Ifbase address of array is 1000

p = 1008 9=p+
iq = 44 = 1024 = 1008 + 4x4

NESO ACADEMY
IN REALITY...

I 1 2 3 4 5 6 7
Ifbase address of array is 1000

q- p = 4 1024 - 1008 = 484

NESO ACADEMY
IN REALITY..

I 7
1 2 3 4 5 6
Number
If baseaddress of array is 1000
of bytes
in

9- p = 4 1024 - 1008 =4 integer

4
NESO ACADEMY
UNDEFINED BEHAVIOURS

Performing arithmeticon pointers which are not pointing to array element


leadsto undefined behaviour

int main() {

}
int i =

;
10;
int *p = &i;
printf("%d",
return
*(p+3));
Different outputs everytime

NESO ACADEMY
UNDEFINED BEHAVIOURS

If twopointers are pointing


to different arraysthen performing subtraction
between them leadsto undefined behaviour

Undefined Behaviour

int main() {
Online:
int a[] {1, 2, 3, 4};
int b[] = {10, 20, 30, 40}; 7
int *p = &a[0];
int *q = &b[3];
Offine:
printf("%d", q - p);
return 0;
}

NESO ACADEMY

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy