0% found this document useful (0 votes)
3 views

12 Pointers

pointers C PPT

Uploaded by

kingarnav2410
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 views

12 Pointers

pointers C PPT

Uploaded by

kingarnav2410
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/ 263

BITS Pilani

K K Birla Goa Campus

CSF111: Computer Programming

Pointers

Swaroop Joshi

2023
Down the memory lane…
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76; We didn’t use the actual address at all!


printf("%d", marks);
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76; We didn’t use the actual address at all!


printf("%d", marks);

But what if we could …


A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);

printf(“%p\n\n”, &marks);
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);

printf(“%p\n\n”, &marks);

& gets the address of the variable


That’s why you see it in scanf, where you
want to put a value at the given address
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);

printf(“%p\n\n”, &marks);

%p is the format speci er for addresses


fi
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);

printf(“%p\n\n”, &marks);

> ./a.out
76
0x16b85b354
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);

printf(“%p\n\n”, &marks);

> ./a.out
76 This value can be different on different machines;
0x16b85b354 Can also be different the next time you run it on the same machine!
A box for an int

76
Down the memory lane… Address: 2008
Name: marks

int marks = 76;


printf("%d", marks);

printf(“%p\n\n”, &marks);

> ./a.out
76
0x16b85b354
Detour: hexadecimal numbers

✤ We are familiar with the decimal number system

✤ It uses the Hindu-Arabic numerals 0, 1, …, 9

✤ You have also heard of the binary number system with two numerals: 0, 1

✤ In general, you can represent the same number in an n-ary number system
with numerals 0, …, n-1

✤ Hexadecimal number system uses 16 symbols: 0,…, 9, a,…,f

✤ It’s just another form of representing numbers that is used to show


memory addresses in C
Detour: hexadecimal numbers
Dec. Hex.
✤ We are familiar with the decimal number system
0 0
✤ It uses the Hindu-Arabic numerals 0, 1, …, 9 1 1
9 9
✤ You have also heard of the binary number system with two numerals: 0, 1
10 a
✤ In general, you can represent the same number in an n-ary number system 11 b
with numerals 0, …, n-1 15 f
✤ Hexadecimal number system uses 16 symbols: 0,…, 9, a,…,f 16 10
17 11
✤ It’s just another form of representing numbers that is used to show
31 1f
memory addresses in C
32 20
You don’t have to
Detour: hexadecimal numbers remember this!

Dec. Hex.
✤ We are familiar with the decimal number system
0 0
✤ It uses the Hindu-Arabic numerals 0, 1, …, 9 1 1
9 9
✤ You have also heard of the binary number system with two numerals: 0, 1
10 a
✤ In general, you can represent the same number in an n-ary number system 11 b
with numerals 0, …, n-1 15 f
✤ Hexadecimal number system uses 16 symbols: 0,…, 9, a,…,f 16 10
17 11
✤ It’s just another form of representing numbers that is used to show
31 1f
memory addresses in C
32 20
Back to the memory
lane…
Relevant portion of memory (‘data area’) allotted to main

Back to the memory


lane…
Relevant portion of memory (‘data area’) allotted to main

Back to the memory


lane…
int marks = 76;
printf("%d\n", marks);
printf("%p\n", &marks);
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
int marks = 76;
printf("%d\n", marks);
printf("%p\n", &marks);
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
int marks = 76;
printf("%d\n", marks);
printf("%p\n", &marks); Computer nds an address within the
memory where it can keep a box big enough
to store the speci ed type and notes down
its name, type, and address
fi
fi
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
int marks = 76;
printf("%d\n", marks);
printf("%p\n", &marks);

76
0x16d12b354
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
int marks = 76;
printf("%d\n", marks);
printf("%p\n", &marks);

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);

76
0x16d12b354
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);

76
0x16d12b354
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);

76
0x16d12b354
y
0x16d12b350
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);

double area = M_PI * 10 * 10;


printf("%lf\n", area);
printf("%p\n", &area);

76
0x16d12b354
y
0x16d12b350
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n", &area);
0x16f25b344
76
0x16d12b354
y
0x16d12b350
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n", &area);
0x16f25b344
76
0x16d12b354
y
0x16d12b350
314.159265
0x16d12b344
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… Note, no matter what the variable type is,
0x16f25b354
ans: char

address is always printed with %p y


int marks = 76;
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n", &area);
0x16f25b344
76
0x16d12b354
y
0x16d12b350
314.159265
0x16d12b344
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n", &area);
0x16f25b344
76
0x16d12b354
It’s cool to see where your values are stored,
but that doesn’t seem very helpful
y
0x16d12b350
314.159265
0x16d12b344
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n", &area);
0x16f25b344
76
0x16d12b354
It’s cool to see where your values are stored,
but that doesn’t seem very helpful
y
0x16d12b350
What if we can hold the address in a
314.159265
0x16d12b344 variable and pass it around?
Relevant portion of memory (‘data area’) allotted to main

Back to the memory marks: int

76
lane… 0x16f25b354
ans: char

int marks = 76; y


printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n", &area);
0x16f25b344
76
0x16d12b354
y
0x16d12b350
314.159265
0x16d12b344
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350

area: double

76 314.159265
0x16f25b354
0x16f25b344
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

area: double

76 314.159265
0x16f25b354
0x16f25b344
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

area: double

76 314.159265
0x16f25b354
0x16f25b344
marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks_ptr is a variable like many others. marks: int

It has a type, a value, and an address 76


ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

area: double

76 314.159265
0x16f25b354
0x16f25b344
marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

area: double

Its value is the address of


76 314.159265
0x16f25b354 another variable, marks
0x16f25b344
marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks); This creates a special relation
printf("%p\n", &marks); 0x16d12b350
between the two:
int *marks_ptr = &marks; marks_ptr “points to” marks
printf("%d\n", *marks_ptr);

area: double

76 314.159265
0x16f25b354
0x16f25b344
marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks; * * is a pointer dereference operator;
printf("%d\n", *marks_ptr);
* Gets the “value at the address” it is applied to
area: double

76 314.159265
0x16f25b354
0x16f25b344
marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

area: double

76 314.159265
0x16f25b354
0x16f25b344
76 marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

printf("%p\n", marks_ptr); area: double


printf("%p\n", &marks_ptr);

76 314.159265
0x16f25b354
0x16f25b344
76 marks_ptr: int*

0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

printf("%p\n", marks_ptr); area: double


printf("%p\n", &marks_ptr);

76 314.159265
0x16f25b354
You can print/use its value, i.e., address 0x16f25b344
of marks, without the *
76 marks_ptr: int*
And its address using the &
0x16f25b354
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

printf("%p\n", marks_ptr); area: double


printf("%p\n", &marks_ptr);

76 314.159265
0x16f25b354
0x16f25b344
76 marks_ptr: int*

0x16f25b354
0x16f25b354
0x16f663368
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

printf("%p\n", marks_ptr); area: double


printf("%p\n", &marks_ptr);

76 314.159265
0x16f25b354
0x16f25b344
76 marks_ptr: int*
Ok, again, pretty cool,
0x16f25b354
0x16f25b354 but what’s the real point
0x16f663368 of pointers?
0x16f663368
Data area of main

marks: int

76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n", &marks); 0x16d12b350
int *marks_ptr = &marks;
printf("%d\n", *marks_ptr);

printf("%p\n", marks_ptr); area: double


printf("%p\n", &marks_ptr);

76 314.159265
0x16f25b354
0x16f25b344
76 marks_ptr: int*

0x16f25b354
0x16f25b354
0x16f663368
0x16f663368
Data area of update_rank

void update_rank(int rank, int change)


{
rank += change; Data area of main
}
int main() {
int my_rank = 7;

update_rank(my_rank, -1);
printf("Rank: %d\n”, my_rank);

return 0;
}
Data area of update_rank

void update_rank(int rank, int change)


{
rank += change; Data area of main
} What is the output of
int main() { this program?
int my_rank = 7;

update_rank(my_rank, -1);
printf("Rank: %d\n”, my_rank);

return 0;
}
Data area of update_rank

void update_rank(int rank, int change)


{
rank += change; Data area of main
}
int main() {
int my_rank = 7; my_rank: int

update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank);
0x16b743358
return 0;
}
Data area of update_rank

void update_rank(int rank, int change)


{
rank += change; Data area of main
}
int main() {
int my_rank = 7; When update_rank
my_rank: int is called, two new variables,
rank and change, are created in its data area and
update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank); new copies of values of expressions my_rank
0x16b743358 and -1 are placed in them
return 0;
}
Data area of update_rank

rank: int change: int

7 -1
0x16f7d732c 0x16f7d7328

void update_rank(int rank, int change)


{
rank += change; Data area of main
}
int main() {
int my_rank = 7; my_rank: int

update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank);
0x16b743358
return 0;
}
Data area of update_rank

rank: int change: int

67 -1
0x16f7d732c 0x16f7d7328

void update_rank(int rank, int change)


{
rank += change; Data area of main
}
int main() {
int my_rank = 7; my_rank: int

update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank);
0x16b743358
return 0;
}
Data area of update_rank

The data area of the function is erased


once it returns (remember ‘stack of rank: int change: int
tracing tables’?). All variables inside it
vanish! 67 -1
0x16f7d732c 0x16f7d7328

void update_rank(int rank, int change)


{
rank += change; Data area of main
}
int main() {
int my_rank = 7; my_rank: int

update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank);
0x16b743358
return 0;
}
void update_rank(int rank, int change)
{
rank += change; Data area of main
}
int main() {
int my_rank = 7; my_rank: int

update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank);
0x16b743358
return 0;
}
void update_rank(int rank, int change)
{
rank += change; Data area of main
}
int main() {
int my_rank = 7; my_rank: int

update_rank(my_rank, -1); 7
printf("Rank: %d\n”, my_rank);
0x16b743358
return 0;
}
Rank: 7
int main() {
int my_rank = 7;

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank);

return 0;
}
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}

int main() {
int my_rank = 7;

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank);

return 0;
}
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}
What is the output of
int main() { this program?
int my_rank = 7;

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank);

return 0;
}
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 7
0x16b743358
return 0;
}
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change; When update_rank_p is called, two new
} variables, ptr and change, are created in its
Data area of main
data area and new copies of values of
int main() { expressions &my_rank and -1 are placed in
int my_rank = 7;
them my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 7
0x16b743358
return 0;
}
Data area of update_rank_p

ptr: int*
change: int
0x16b743358
-1
0x16b0e7328
void update_rank_p(int *ptr, int change)
0x16f7d7328
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 7
0x16b743358
return 0;
}
Data area of update_rank_p

ptr: int*
change: int
0x16b743358
-1
0x16b0e7328
void update_rank_p(int *ptr, int change)
0x16f7d7328
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 7
0x16b743358
return 0;
}
Data area of update_rank_p

ptr: int*
change: int
0x16b743358
-1
0x16b0e7328
void update_rank_p(int *ptr, int change)
0x16f7d7328
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank
Value = 7; pointed to by ptr is
of the variable my_rank: int
replaced by (the value of the variable
update_rank_p(&my_rank, -1);
pointed to by %d\n”,
printf("Rank: ptr + value of change)
my_rank); 7
0x16b743358
return 0;
}
Data area of update_rank_p

ptr: int*
change: int
0x16b743358
-1
0x16b0e7328
void update_rank_p(int *ptr, int change)
0x16f7d7328
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 67
0x16b743358
return 0;
}
Data area of update_rank_p

The data area of the function is erased once ptr: int*


it returns (remember ‘stack of tracing change: int
tables’?). All variables inside it vanish! 0x16b743358
-1
0x16b0e7328
void update_rank_p(int *ptr, int change)
0x16f7d7328
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 67
0x16b743358
return 0;
}
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 67
0x16b743358
return 0;
}
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 67
0x16b743358
return 0; But the change persists since it
}
has modi ed the variable in
the calling function
fi
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 67
0x16b743358
return 0;
}
Rank: 6
void update_rank_p(int *ptr, int change)
{
*ptr = *ptr + change;
}
Data area of main

int main() {
int my_rank = 7;
my_rank: int

update_rank_p(&my_rank, -1);
printf("Rank: %d\n”, my_rank); 67
Point of using pointers 1: 0x16b743358
return 0;
} Pointers allow you to modify
Rank: 6 variables outside their scope
More use of pointers

✤ I get super annoyed when I can’t get ₹ 100 notes easily – ATMs only seem to give ₹ 500
ones!

✤ Suppose you are hired by a bank to update their ATM program.

✤ Write a function that receives an integer input that is a multiple of 100 and outputs the
number of ₹ 500 notes, ₹ 200 notes, and ₹ 100 notes it should dispense. (Always
maximise the higher denomination notes.)

✤ E.g.: Input = 900 → Output = 1x₹ 500, 2x₹ 200

✤ E.g.: Input = 800 → Output = 1x₹ 500, 1x₹ 200, 1x₹ 100
More use of pointers

✤ I get super annoyed when I can’t get ₹ 100 notes easily – ATMs only seem to give ₹ 500
ones! How can you ever return three things
from a function?
✤ Suppose you are hired by a bank to update their ATM program.

✤ Write a function that receives an integer input that is a multiple of 100 and outputs the
number of ₹ 500 notes, ₹ 200 notes, and ₹ 100 notes it should dispense. (Always
maximise the higher denomination notes.)

✤ E.g.: Input = 900 → Output = 1x₹ 500, 2x₹ 200

✤ E.g.: Input = 800 → Output = 1x₹ 500, 1x₹ 200, 1x₹ 100
More use of pointers

✤ I get super annoyed when I can’t get ₹ 100 notes easily – ATMs only seem to give ₹ 500
ones!

✤ Suppose you are hired by a bank to update their ATM program.

✤ Write a function that receives an integer input that is a multiple of 100 and outputs the
number of ₹ 500 notes, ₹ 200 notes, and ₹ 100 notes it should dispense. (Always
maximise the higher denomination notes.)
You cannot!
✤ E.g.: Input = 900 → Output = 1x₹ 500, 2x₹ 200 But you can pass three pointers to a
function and ask it to put the relevant
✤ E.g.: Input = 800 → Output = 1x₹ 500, 1x₹ 200, 1x₹ 100 values in them!
More use of pointers

✤ I get super annoyed when I can’t get ₹ 100 notes easily – ATMs only seem to give ₹ 500
ones!

✤ Suppose you are hired by a bank to update their ATM program.

✤ Write a function that receives an integer input that is a multiple of 100 and outputs the
number of ₹ 500 notes, ₹ 200 notes, and ₹ 100 notes it should dispense. (Always
maximise the higher denomination notes.)

✤ E.g.: Input = 900 → Output = 1x₹ 500, 2x₹ 200

✤ E.g.: Input = 800 → Output = 1x₹ 500, 1x₹ 200, 1x₹ 100
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{

}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{

Three “output parameters”


void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{

}
int main()
{
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200;
total = total % 200;
*p100 = total / 100;
}
int main()
{
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int
}
int main() 800
{
some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int
}
int main() 800
{
some_addr
int amount = 800;
int notes500, notes200, notes100;

We don’t really
atm_dispensing(amount, &notes500, &notes200, care what the
&notes100);
exact address is!
printf("Dispense %d as %dx500 + %dx200 + %dx100\n",
amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int
}
int main() 800 ?
{
some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int
}
int main() 800 ?
{
some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);


Even for the variables we are sending
to the pointers; because the behaviour
printf("Dispense %d as %dx500 + %dx200 + %dx100\n",
amount, notes500, notes200, notes100); is independent of the actual address!
return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int
}
int main() 800 ? ?
{
some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 ? ? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 ? ? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
The function call creates three
}
pointer relations
void atm_dispensing(int total, Data area of atm_dispensing
int *p500,
int *p200, total: int p500: int* p200: int* p100: int*
int *p100)
{ 800
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 ? ? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total, Data area of atm_dispensing
int *p500,
int *p200, total: int p500: int* p200: int* p100: int*
int *p100)
{ 800
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? ? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total, Data area of atm_dispensing
int *p500,
int *p200, total: int p500: int* p200: int* p100: int*
int *p100)
{ 300
800
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? ? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total, Data area of atm_dispensing
int *p500,
int *p200, total: int p500: int* p200: int* p100: int*
int *p100)
{ 300
800
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? 1? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total, Data area of atm_dispensing
int *p500,
int *p200, total: int p500: int* p200: int* p100: int*
int *p100)
{ 100
300
800
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? 1? ?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total, Data area of atm_dispensing
int *p500,
int *p200, total: int p500: int* p200: int* p100: int*
int *p100)
{ 100
300
800
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? 1? 1?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? 1? 1?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? 1? 1?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500, notes200, notes100);

return 0;
}
Dispense 800 as 1x500 + 1x200 + 1x100
void atm_dispensing(int total,
int *p500,
int *p200,
int *p100)
{
*p500 = total / 500;
total = total % 500;
*p200 = total / 200; Data area of main
total = total % 200;
*p100 = total / 100; amount: int notes500: int notes200: int notes100: int
}
int main() 800 1? 1? 1?
{
some_addr some_addr some_addr some_addr
int amount = 800;
int notes500, notes200, notes100;

atm_dispensing(amount, &notes500, &notes200, &notes100);

printf("Dispense %d as %dx500 + %dx200 + %dx100\n",


amount, notes500,
Point ofnotes200, notes100);
using pointers 2:
Pointers allow you to receive
return 0;
} multiple results from a function
Dispense 800 as 1x500 + 1x200 + 1x100
Types of functions seen so far

Purpose Return type Parameters Result returned


int, bool, double, char, Input params only; Copied back via the
Compute a single value
etc. Hold copies of data return statement
from the calling
Print output to console void function No results returned

Input params - as
above;
Compute multiple Stored in the output
void Output params-
values params
pointers to actual
arguments
Types of functions seen so far

Purpose Return type Parameters Result returned


int, bool, double, char, Input params only; Copied back via the
Compute a single value
etc. Hold copies of data return statement
from the calling
Print
Try output to such
to create console
functions as void function No results returned
much as possible
Input params - as
above;
Compute multiple Stored in the output
void Output params-
values params
pointers to actual
arguments
Types of functions seen so far

Purpose Return type Parameters Result returned


int, bool, double, char, Input params only; Copied back via the
Compute a single value
etc. Hold copies of data return statement
from the calling
Print output to console void function No results returned

Input params - as
above;
Compute multiple Stored in the output
void Output params-
values params
pointers to actual
arguments
Your turn

✤ Suppose you are hired by a recruiting rm. They often receive a list of CGPAs from colleges
and have to determine how many interviews to hold based on some cutoff.

✤ Write a function that, given a list of real numbers representing student CGPAs, outputs the
number of students whose CGPA is

✤ Above 9

✤ Between 7.5 inclusive and 9 exclusive

✤ Between 6 inclusive and 7.5 exclusive

✤ Below 6
fi
BITS Pilani
K K Birla Goa Campus

CSF111: Computer Programming

Pointers

Swaroop Joshi

2023
Size matters!
char

int

double
A variable of a type takes specific space to store
A variable of a type takes specific space to store
typedef struct
{
char author[20];
char text[140];
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
} tweet_t;
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
} tweet_t;
A variable of a type takes specific space to store
The trailing elements of a struct, if
left unspeci ed, are automatically
typedef struct char response = 'n'; set to 0 - if numeric; “”- if strings
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt;
int num_likes;
} tweet_t;
fi
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes;
} tweet_t;
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes;
} tweet_t;
sizeof is an operator that computes the
number of bytes the variable or the type it
is applied to takes in memory
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;

These values can differ


from machine to machine
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A double takes 8 bytes
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A double takes 8 bytes
printf("A tweet_t takes %d bytes\n", (int) sizeof t1);
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A double takes 8 bytes
printf("A tweet_t takes %d bytes\n", (int) sizeof t1);
A tweet_t takes 212 bytes
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; 20x1 (author)
double discount = .1;
char text[140]; +140x1 t1
tweet_t (text)
= {"@bitsgoa", "End of 2022-23"};
char timestamp[40]; +40x1 (timestamp)
int num_rt;
int num_quote_rt; +4 (num_rt)
printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A +4 (num_quote_rt)
char takes 1 bytes
} tweet_t;
+4 (num_likes)
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A double takes 8 bytes
printf("A tweet_t takes %d bytes\n", (int) sizeof t1);
A tweet_t takes 212 bytes
A variable of a type takes specific space to store
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A double takes 8 bytes
printf("A tweet_t takes %d bytes\n", (int) sizeof t1);
A tweet_t takes 212 bytes
Important: size of a struct is not
A variable of a type takes specific space to store
always the same as the sum of sizes
of its elements; details are out of
scope of this course
typedef struct char response = 'n';
{ int num = 42;
char author[20]; double discount = .1;
char text[140]; tweet_t t1 = {"@bitsgoa", "End of 2022-23"};
char timestamp[40];
int num_rt;
int num_quote_rt; printf("A char takes %d bytes\n", (int) sizeof response);
int num_likes; A char takes 1 bytes
} tweet_t;
printf("An int takes %d bytes\n", (int) sizeof num);
An int takes 4 bytes
printf("A double takes %d bytes\n", (int) sizeof discount);
A double takes 8 bytes
printf("A tweet_t takes %d bytes\n", (int) sizeof t1);
A tweet_t takes 212 bytes
A pointer always takes the same space
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
The speci c value can
differ from machine to
machine
fi
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
printf("An int* takes %d bytes\n", (int) sizeof num_p);
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
printf("An int* takes %d bytes\n", (int) sizeof num_p);
An int* takes 8 bytes
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
printf("An int* takes %d bytes\n", (int) sizeof num_p);
An int* takes 8 bytes
printf("A double* takes %d bytes\n", (int) sizeof discount_p);
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
printf("An int* takes %d bytes\n", (int) sizeof num_p);
An int* takes 8 bytes
printf("A double* takes %d bytes\n", (int) sizeof discount_p);
A double* takes 8 bytes
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
printf("An int* takes %d bytes\n", (int) sizeof num_p);
An int* takes 8 bytes
printf("A double* takes %d bytes\n", (int) sizeof discount_p);
A double* takes 8 bytes
printf("A tweet_t* takes %d bytes\n", (int) sizeof t1_p);
A pointer always takes the same space
char *response_p = &response;
int *num_p = #
double *discount_p = &discount;
tweet_t *t1_p = &t1;

printf("A char* takes %d bytes\n", (int) sizeof response_p);


A char* takes 8 bytes
printf("An int* takes %d bytes\n", (int) sizeof num_p);
An int* takes 8 bytes
printf("A double* takes %d bytes\n", (int) sizeof discount_p);
A double* takes 8 bytes
printf("A tweet_t* takes %d bytes\n", (int) sizeof t1_p);
A tweet_t* takes 8 bytes
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = {
"@bitsgoa",
"Not for the first years!”,
“2023-05-19", 20, 30, 40};

tweet_t popular = more_liked_tweet(t1, t2);


printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t
"@bitsgoa", 212 bytes
"Not for the first years!”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; 212Data
bytes
area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2) tw1: tweet_t
{
tweet_t ans = (tw1.num_likes “@bitsgoa”,
> tw2.num_likes) “End of…”
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2) tw1: tweet_t
{
tweet_t ans = (tw1.num_likes “@bitsgoa”,
> tw2.num_likes) “End of…”
? tw1
: tw2;
return ans; The entire value of t1
} is copied to tw1;
int main() So 212 bytes
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2) tw1: tweet_t tw2: tweet_t
{
tweet_t ans = (tw1.num_likes “@bitsgoa”, “@bitsgoa”,
> tw2.num_likes) “End of…” “Not for…”
? tw1
: tw2;
return ans;
} Again 212 bytes
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2) tw1: tweet_t tw2: tweet_t
{ Also 212 bytes
tweet_t ans = (tw1.num_likes “@bitsgoa”, “@bitsgoa”,
> tw2.num_likes) “End of…” “Not for…”
? tw1
: tw2; ans: tweet_t
return ans;
} “@bitsgoa”,
int main() “Not for…”
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2);
printf("Tweet '%s' has more likes\n”,
popular.text);

return 0;
}
Data area of more_liked_tweet
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2) tw1: tweet_t tw2: tweet_t
{
tweet_t ans = (tw1.num_likes “@bitsgoa”, “@bitsgoa”,
> tw2.num_likes) “End of…” “Not for…”
? tw1
: tw2; ans: tweet_t
return ans;
} “@bitsgoa”,
int main() “Not for…”
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2); popular: tweet_t
printf("Tweet '%s' has more likes\n”,
popular.text); “@bitsgoa”,
return 0;
“Not for…”
}
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2); popular: tweet_t
printf("Tweet '%s' has more likes\n”,
popular.text); The whole thing is copied “@bitsgoa”,
back here, so again, 212 bytes “Not for…”
return 0;
}
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = {
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2); popular: tweet_t
printf("Tweet '%s' has more likes\n”,
popular.text); “@bitsgoa”,
return 0;
“Not for…”
}
tweet_t more_liked_tweet(tweet_t tw1,
tweet_t tw2)
{
tweet_t ans = (tw1.num_likes
> tw2.num_likes)
? tw1
: tw2;
return ans;
}
int main()
{
tweet_t t1 = { Can we do better using pointers?
“@bitsgoa”,
"End of 2022-23"}; Data area of main
tweet_t t2 = { t1: tweet_t t2: tweet_t
"@bitsgoa",
"Not for the first years!”, “@bitsgoa”, “@bitsgoa”,
“2023-05-19", 20, 30, 40};
“End of…” “Not for…”
tweet_t popular = more_liked_tweet(t1, t2); popular: tweet_t
printf("Tweet '%s' has more likes\n”,
popular.text); “@bitsgoa”,
return 0;
“Not for…”
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … };

tweet_t popular_p
= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”,
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t

tweet_t popular_p “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t
212 bytes
tweet_t popular_p “@bitsgoa”,
= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; 212Data
bytes
area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p) t1_p: tweet_t*
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p) t1_p: tweet_t*
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
8 bytes
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p) t1_p: tweet_t* t2_p: tweet_t*
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans; 8 bytes
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p) t1_p: tweet_t* t2_p: tweet_t*
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p) t1_p: tweet_t* t2_p: tweet_t*
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p); ans: tweet_t
return ans;
} “@bitsgoa”,
“Not for…”
int main()
{ Copied
tweet_t t1 = { … }; Data area of main from here
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
Data area of more_liked_tweet_p
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p) t1_p: tweet_t* t2_p: tweet_t*
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p); ans: tweet_t
return ans;
} “@bitsgoa”,
“Not for…”
int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text);

return 0;
}
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text); popular: tweet_t

return 0; “@bitsgoa”,
} 212 bytes
“Not for…”
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

int main()
{
tweet_t t1 = { … }; Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text); popular: tweet_t

return 0; “@bitsgoa”,
}
“Not for…”
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

Point of using pointers 3:


int main() Pointers allow you to pass values
{
tweet_t t1 = { … }; between functions more ef ciently Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


= more_liked_tweet_p(&t1, &t2);
printf("Tweet '%s' has more likes\n”, “End of…” “Not for…”
popular_p.text); popular: tweet_t

return 0; “@bitsgoa”,
}
“Not for…”
fi
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

Point of using pointers 3:


int main() Pointers allow you to pass values
{
tweet_t t1 = { … }; between functions more ef ciently Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


Can we do even better by
= more_liked_tweet_p(&t1, &t2);
printf("Tweet “End of…” “Not for…”
returning a'%s' has more likes\n”,
pointer?
popular_p.text); popular: tweet_t

return 0; “@bitsgoa”,
}
“Not for…”
fi
tweet_t more_liked_tweet_p(tweet_t *t1_p,
tweet_t *t2_p)
{
tweet_t ans = ((*t1_p).num_likes
> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
return ans;
}

Point of using pointers 3:


int main() Pointers allow you to pass values
{
tweet_t t1 = { … }; between functions more ef ciently Data area of main
tweet_t t2 = { … }; t1: tweet_t t2: tweet_t

tweet_t popular_p “@bitsgoa”, “@bitsgoa”,


Can we do even better by
= more_liked_tweet_p(&t1, &t2);
printf("Tweet “End of…” “Not for…”
returning a'%s' has more likes\n”,
pointer?
popular_p.text); popular: tweet_t

return 0; “@bitsgoa”,
} Yes, but be patient! :-)
“Not for…”
fi
The arrow operator

✤ Member access operator

✤ ptr_name –> member_name

✤ Is equivalent to

✤ (*ptr_name).member_name

✤ Cannot apply on a non-pointer


variable
tweet_t more_liked_tweet_p(tweet_t *t1_p,
The arrow operator {
tweet_t *t2_p)

tweet_t ans = ((*t1_p).num_likes


> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
✤ Member access operator return ans;
}
✤ ptr_name –> member_name

✤ Is equivalent to

✤ (*ptr_name).member_name

✤ Cannot apply on a non-pointer


variable
tweet_t more_liked_tweet_p(tweet_t *t1_p,
The arrow operator {
tweet_t *t2_p)

tweet_t ans = ((*t1_p).num_likes


> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
✤ Member access operator return ans;
}
✤ ptr_name –> member_name
tweet_t more_liked_tweet_p(tweet_t *t1_p,
✤ Is equivalent to tweet_t *t2_p)
{
tweet_t ans = (t1_p->num_likes
> t2_p->num_likes)
✤ (*ptr_name).member_name ? (*t1_p)
: (*t2_p);
return ans;
✤ Cannot apply on a non-pointer }
variable
tweet_t more_liked_tweet_p(tweet_t *t1_p,
The arrow operator {
tweet_t *t2_p)

tweet_t ans = ((*t1_p).num_likes


> (*t2_p).num_likes)
? (*t1_p)
: (*t2_p);
✤ Member access operator return ans;
}
✤ ptr_name –> member_name
tweet_t more_liked_tweet_p(tweet_t *t1_p,
✤ Is equivalent to tweet_t *t2_p)
{
tweet_t ans = (t1_p->num_likes
> t2_p->num_likes)
✤ (*ptr_name).member_name ? (*t1_p)
: (*t2_p);
Preferred
return ans;
✤ Cannot apply on a non-pointer }
variable
BITS Pilani
K K Birla Goa Campus

CSF111: Computer Programming

Pointers

Swaroop Joshi

2023
Pointer arithmetic
Data area of main

marks: int

Recall 76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n\n", &area);
0x16f25b344
76
0x16d12b354
y
0x16d12b350
314.159265
0x16d12b344
Data area of main

marks: int

Recall 76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x16d12b350

char ans = 'y';


printf("%c\n", ans);
printf("%p\n\n", &ans);
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n\n", &area);
0x16f25b344
76 We are showing the address values
0x16d12b354
temporarily to understand the
y concept; we will again hide this detail
0x16d12b350
once we cover the concept
314.159265
0x16d12b344
Data area of main

marks: int

Recall 76
ans: char
0x16f25b354
int marks = 76; y
printf("%d\n", marks);
printf("%p\n\n", &marks); Remember: these addresses are only 0x16d12b350
for representation purpose; actual
char ans = 'y';
printf("%c\n", ans); addresses can be different when you
printf("%p\n\n", &ans); run it on your machine
area: double
double area = M_PI * 10 * 10;
printf("%lf\n", area); 314.159265
printf("%p\n\n", &area);
0x16f25b344
76
0x16d12b354
y
0x16d12b350
314.159265
0x16d12b344
Under the hood
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 y 0x..60
y
0x..51 0x..61
0x16d12b350
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
double area = M_PI * 10 * 10; 0x..4c 0x..5c
printf("%lf\n", area); 0x..4d 0x..5d
printf("%p\n\n", &area);
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 y 0x..60
y
0x..51 0x..61
0x16d12b350
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 314.159 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 265 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
double area = M_PI * 10 * 10; 0x..4c 0x..5c
printf("%lf\n", area); 0x..4d 0x..5d
printf("%p\n\n", &area);
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 y 0x..60
y
0x..51 0x..61
0x16d12b350
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 314.159 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 265 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
double area = M_PI * 10 * 10; 0x..4c 0x..5c
printf("%lf\n", area); 0x..4d 0x..5d
printf("%p\n\n", &area);
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 y 0x..60
y
0x..51 0x..61
0x16d12b350
0x..52 0x..62
314.159265
0x16d12b344 0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 314.159 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 265 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
double area = M_PI * 10 * 10; 0x..4c 0x..5c
printf("%lf\n", area); 0x..4d 0x..5d
printf("%p\n\n", &area);
Pointers are numbers, so you can 0x..4e 0x..5e
76 perform (some) arithmetic 0x..4f 0x..5f
0x16d12b354
operations with them! 0x..50 y 0x..60
y
0x..51 0x..61
0x16d12b350
0x..52 0x..62
314.159265
0x16d12b344 0x..53 ...
Data area of main

Address Value Address Value


Under the hood 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int marks = 76; 0x..47 314.159 0x..57
printf("%d\n", marks);
printf("%p\n\n", &marks); 0x..48 265 0x..58
0x..49 0x..59
char ans = 'y';
printf("%c\n", ans); 0x..4a 0x..5a
printf("%p\n\n", &ans); 0x..4b 0x..5b
double area = M_PI * 10 * 10; 0x..4c 0x..5c
printf("%lf\n", area); 0x..4d 0x..5d
printf("%p\n\n", &area);
0x..4e 0x..5e
76 0x..4f 0x..5f
0x16d12b354
0x..50 y 0x..60
y
0x..51 0x..61
0x16d12b350
0x..52 0x..62
314.159265
0x16d12b344 0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 What does this line print? 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
54 + 1 ≠ 55!
0x..4c 0x..5c
0x16d12b358
When p is a0x..4d
pointer, 0x..5d
p + k = p + k *0x..4e
sizeof (*p) 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
Access the next int,
265
printf("%p\n", marks_p); 0x..48 0x..58
not the next byte
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
printf("%d\n", *(marks_p + 1)); 0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
printf("%d\n", *(marks_p + 1)); 0x..4e 0x..5e
0x..4f 0x..5f
32127 0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
Or*(marks_p
printf("%d\n", some other+value, 0x..4e
1)); depending the the 0x..5e
0x..4f
bits lying there – there is always some 0x..5f
32127 value, we just can’t reason about it!0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
printf("%d\n", *(marks_p + 1)); 0x..4e 0x..5e
0x..4f 0x..5f
32127 0x..50 y 0x..60
*(marks_p + 1) = 42; 0x..51 0x..61
printf("%d\n", *(marks_p + 1));
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
printf("%d\n", *(marks_p + 1)); 0x..4e 0x..5e
0x..4f 0x..5f
32127 0x..50 y 0x..60
*(marks_p + 1) = 42; 0x..51 0x..61
printf("%d\n", *(marks_p + 1)); You can manipulate those bits!
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
printf("%d\n", *(marks_p + 1)); 0x..4e 0x..5e
0x..4f 0x..5f
32127 0x..50 y 0x..60
*(marks_p + 1) = 42; 0x..51 0x..61
printf("%d\n", *(marks_p + 1)); You can manipulate those bits!
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


The next integer 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p + 1); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b358
0x..4d 0x..5d
printf("%d\n", *(marks_p + 1)); 0x..4e 0x..5e
0x..4f 0x..5f
32127 0x..50 y 0x..60
*(marks_p + 1) = 42; 0x..51 0x..61
printf("%d\n", *(marks_p + 1));
0x..52 0x..62
42 0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 What does this line print? 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p - 3); 0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p - 3); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b348
0x..4d 0x..5d
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p - 3); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b348
0x..4d 0x..5d
printf("%d\n", *(marks_p - 3)); 0x..4e 0x..5e
0x..4f 0x..5f
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p - 3); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b348
0x..4d 0x..5d
printf("%d\n", *(marks_p - 3)); 0x..4e 0x..5e
0x..4f 0x..5f
1081320076 0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p - 3); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b348
0x..4d 0x..5d
Integer representation of the 0x..4e 0x..5e
printf("%d\n", *(marks_p - 3));
second half of the bits that make0x..4f
up 0x..5f
1081320076 314.159265 – makes no sense!
0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


You can lose type info. 0x..44
0x..45
0x..54
0x..55
76
0x..46 0x..56
int *marks_p = &marks; 0x..47 314.159 0x..57
printf("%p\n", marks_p); 0x..48 265 0x..58
0x16d12b354 0x..49 0x..59
42
0x..4a 0x..5a
printf("%p\n", marks_p - 3); 0x..4b 0x..5b
0x..4c 0x..5c
0x16d12b348
0x..4d 0x..5d
printf("%d\n", *(marks_p - 3)); 0x..4e 0x..5e
0x..4f 0x..5f
1081320076 0x..50 y 0x..60
0x..51 0x..61
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
20 60
0x..4a 0x..5a
0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
30 70
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
Assuming num_p holds the address of the
0x..47 0x..57
integer 10 in the given illustration,
what does this code print? 0x..48 0x..58
0x..49 0x..59
20 60
for (int i = 0; i < 6; ++i) 0x..4a 0x..5a
printf("%d ", *(num_p + i)); 0x..4b 0x..5b
0x..4c 0x..5c
0x..4d 0x..5d
30 70
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
20 60
for (int i = 0; i < 6; ++i) 0x..4a 0x..5a
printf("%d ", *(num_p + i)); 0x..4b 0x..5b
0x..4c 0x..5c
10 20 30 40 50 60 0x..4d 0x..5d
30 70
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
20 60
for (int i = 0; i < 6; ++i) 0x..4a 0x..5a
What does this code remind you of?
printf("%d ", *(num_p + i)); 0x..4b 0x..5b
0x..4c 0x..5c
10 20 30 40 50 60 0x..4d 0x..5d
30 70
0x..4e 0x..5e
0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
20 60
for (int i = 0; i < 6; ++i) 0x..4a 0x..5a
printf("%d ", *(num_p + i)); 0x..4b 0x..5b
0x..4c 0x..5c
10 20 30 40 50 60 0x..4d 0x..5d
30 70
0x..4e 0x..5e
for (int i = 0; i < 6; ++i)
printf("%d ", num_p[i]); 0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
20 60
for (int i = 0; i < 6; ++i) 0x..4a 0x..5a
printf("%d ", *(num_p + i)); 0x..4b 0x..5b
0x..4c 0x..5c
10 20 30 40 50 60 0x..4d 0x..5d
30 70
0x..4e 0x..5e
for (int i = 0; i < 6; ++i)
printf("%d ", num_p[i]); 0x..4f
These two code 0x..5f
fragments are equivalent!
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Data area of main

Address Value Address Value


Pointers all along! 0x..44
0x..45
0x..54
0x..55
10 50
0x..46 0x..56
0x..47 0x..57
0x..48 0x..58
0x..49 0x..59
20 60
for (int i = 0; i < 6; ++i) 0x..4a 0x..5a
printf("%d ", *(num_p + i)); 0x..4b 0x..5b
0x..4c 0x..5c
10 20 30 40 50 60 0x..4d 0x..5d
30 70
0x..4e 0x..5e
for (int i = 0; i < 6; ++i)
printf("%d ", num_p[i]); 0x..4f 0x..5f
0x..50 0x..60
0x..51 0x..61
40
0x..52 0x..62
0x..53 ...
Array hi pointer hai!

✤ array[i] is actually resolved as *(array + i)


internally!

✤ That’s why there are no bounds to an array


in C Gangadhar hi shaktiman hai!

✤ You could easily crawl over to the 70 in the


previous example.
Array hi pointer hai!

✤ array[i] is actually resolved as *(array + i)


internally!

✤ That’s why there are no bounds to an array


in C Gangadhar hi shaktiman hai!

✤ You could easily crawl over to the 70 in the


… 10 20 30 40 50 60 70 …
previous example.

for (int i = 0; i < 6; ++i)


num_p: int*
printf("%d ", *(num_p + i));
Array hi pointer hai!

✤ array[i] is actually resolved as *(array + i)


internally!

✤ That’s why there are no bounds to an array


in C Gangadhar hi shaktiman hai!

✤ You could easily crawl over to the 70 in the


… 10 20 30 40 50 60 70 …
previous example.

for (int i = 0; i < 6; ++i)


num_p: int*
printf("%d ", *(num_p + i)); We don’t really need the
actual addresses anymore
Array hi pointer hai!

✤ array[i] is actually resolved as *(array + i)


internally!

✤ That’s why there are no bounds to an array


in C Gangadhar hi shaktiman hai!

✤ You could easily crawl over to the 70 in the


… 10 20 30 40 50 60 70 …
previous example.

for (int i = 0; i < 6; ++i)


num_p: int*
printf("%d ", *(num_p + i));
Passing arrays to functions

void print_array(int *num_p, int length)


{
for (int i = 0; i < length; ++i)
printf("%d ", num_p[i]);
printf("\n");
}

int main()
{
int nums[] = {10, 20, 30, 40, 50, 60};
print_array(nums, 6);

return 0;
}
Passing arrays to functions

void print_array(int *num_p, int length)


{
for (int i = 0; i < length; ++i)
printf("%d ", num_p[i]); Same as: void print_array(int nums[], int length)
printf("\n");
}

int main()
{
int nums[] = {10, 20, 30, 40, 50, 60};
print_array(nums, 6);

return 0;
}
Passing arrays to functions

void print_array(int *num_p, int length)


{
for (int i = 0; i < length; ++i)
printf("%d ", num_p[i]); Same as: void print_array(int nums[], int length)
printf("\n");
}

int main()
{
int nums[] = {10, 20, 30, 40, 50, 60};
print_array(nums, 6);

return 0; Same as: print_array(&nums[0], 6);


}
Passing arrays to functions

void print_array(int *num_p, int length)


{
for (int i = 0; i < length; ++i)
Point of usingSame
pointers
as: 4: print_array(int nums[], int length)
void
printf("%d ", num_p[i]);
printf("\n"); Pointers allow you to implement
} aggregate data types like arrays

int main()
{
int nums[] = {10, 20, 30, 40, 50, 60};
print_array(nums, 6);

return 0; Same as: print_array(&nums[0], 6);


}
What does this function do?
bool what_do_i_do(char *s)
{
while (!isdigit(*s) && *s != '\0')
s++;
return isdigit(*s);
}
What does this function do?
bool what_do_i_do(char *s)
{
while (!isdigit(*s) && *s != '\0')
s++; Trace with some examples
return isdigit(*s);
}
What does this function do?
bool what_do_i_do(char *s)
{
while (!isdigit(*s) && *s != '\0')
s++; Trace with some examples
return isdigit(*s);
}

HW: We know that you should not


specify & when taking a string input
using scanf. Can you explain why?
Write a function that computes and displays
the cumulative sum of an array of integers
void print_array(int *nums, int length) Assume this function is available
{
for (int i = 0; i < length; ++i)
printf("%d ", nums[i]);

printf("\n");
}
Write a function that computes and displays
the cumulative sum of an array of integers
void print_array(int *nums, int length) Assume this function is available
{
for (int i = 0; i < length; ++i)
printf("%d ", nums[i]);

printf("\n");
} Does this code do the job?

void print_cumulative_sum(int *nums, int length)


{
for (int i = 1; i < length; ++i)
nums[i] += nums[i - 1];

print_array(nums, length);
}
Write a function that computes and displays
the cumulative sum of an array of integers
void print_array(int *nums, int length) Assume this function is available
{
for (int i = 0; i < length; ++i)
printf("%d ", nums[i]);

printf("\n");
}

void print_cumulative_sum(int *nums, int length)


{
for (int i = 1; i < length; ++i)
nums[i] += nums[i - 1];
Is this code ok?
print_array(nums, length);
}
Write a function that computes and displays
the cumulative sum of an array of integers
void print_array(int *nums, int length) Assume this function is available
{
for (int i = 0; i < length; ++i)
printf("%d ", nums[i]);

printf("\n");
}

void print_cumulative_sum(int *nums, int length)


{
for (int i = 1; i < length; ++i)
nums[i] += nums[i - 1];

print_array(nums, length);
}
Write a function that computes and displays
the cumulative sum of an array of integers
void print_array(int *nums, int length) Assume this function is available
{
for (int i = 0; i < length; ++i)
printf("%d ", nums[i]); We know that pointers allow us to
manipulate variables outside their scope.
printf("\n");
}
Such side effects make it dif cult to reason
about programs, so be careful when writing
void print_cumulative_sum(int *nums, int length)
{ such code!
for (int i = 1; i < length; ++i)
nums[i] += nums[i - 1];

print_array(nums, length);
}
fi
Pointers summary

✤ A pointer is a variable whose value is an address of


another variable in the memory.

✤ Also called indirect reference

✤ Pointers can be dereferenced using the * operator

✤ p–>m is the same as (*p).m where p is a pointer to a struct

✤ Arrays are implemented as pointers

✤ Using pointers can have side effects


BITS Pilani
K K Birla Goa Campus

CSF111
Computer Programming

Pointers
(Supplementary)
Pointers Examples
Write a function that, given a list of real numbers representing student CGPAs, outputs the
number of students whose CGPA is

✤ Above 9
✤ Between 7.5 inclusive and 9 exclusive
✤ Between 6 inclusive and 7.5 exclusive
✤ Below 6
Pointers Examples
Write a function that, given a list of real numbers representing student CGPAs, outputs the
number of students whose CGPA is
✤ Above 9
✤ Between 7.5 inclusive and 9 exclusive
✤ Between 6 inclusive and 7.5 exclusive
✤ Below 6
Pointers Examples
Write a function that, given a list of real numbers representing student CGPAs, outputs the
number of students whose CGPA is

✤ Above 9
✤ Between 7.5 inclusive and 9 exclusive
✤ Between 6 inclusive and 7.5 exclusive
✤ Below 6
Pointers Examples
Swap two integer variables
✤ Passing by value
✤ Passing by address
Pointers Examples
Swap two integer variables
✤ Passing by value
✤ Passing by address

Call by value Call by reference


Pointers Examples
Swap two integer variables
✤ Passing by value
✤ Passing by address
Pointers Examples
Find most liked tweet
✤ returning a structure
✤ returning a pointer to a structure (also inputs as pointer to a structure)
Pointers Examples
Find most liked tweet
✤ returning a structure
✤ returning a pointer to a structure (also inputs as pointer to a structure)
Pointers Examples
Find most liked tweet
✤ returning a structure
✤ returning a pointer to a structure (also inputs as pointer to a structure)

Output

Both works fine, however, the second one is more optimized (in terms of memory usage)!!
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
What will be the output of the code segment?
Revisiting Pointers
Pointers in expressions

If p is an int pointer, then *p is an int variable

If p1 and p2 are two pointers, the following statements are valid:

• sum = (*p1) + (*p2);

• prod = (*p1) * (*p2);

• *p1 = *p1 + 2;

• x = *p1 / *p2 + 5;
Revisiting Pointers
Pointer arithmetic

Arithmetic operations on pointers

What are allowed?

• Add an integer to a pointer.

• Subtract an integer from a pointer.

• Subtract one pointer from another.

• If p1 and p2 are both pointers to the same array, then p2–p1 gives what?
Revisiting Pointers
Pointer arithmetic

Arithmetic operations on pointers

What are allowed?

• Add an integer to a pointer.

• Subtract an integer from a pointer.

• Subtract one pointer from another.

• If p1 and p2 are both pointers to the same array, then p2–p1 gives what?

• the number of elements between p1 and p2.


Revisiting Pointers
Pointer arithmetic

Arithmetic operations on pointers

What are not allowed?

• Add two pointers.

• p1 = p1 + p2;

• Multiply / divide a pointer in an expression.

• p1 = p2 / 5;

• p1 = p1 – p2 * 10;
Revisiting Pointers
Pointer arithmetic

Scale Factor
Revisiting Pointers
Pointer arithmetic

Scale Factor

• Increasing a pointer by 1 does not necessarily increase the pointer by 1 byte, rather it depends on the size of the
data type to which the pointer points.
Revisiting Pointers
Pointer arithmetic

Scale Factor

Data Type Scale Factor


char 1
int 4
float 4
double 8

If p1 is an int pointer, then p1++ will increment the value of p1 by 4.

If p2 is a double pointer, then p2-- will decrement the value of p2 by 8.

Scale factors can be machine dependant.


Revisiting Pointers
Function and pointers
Returning multiple values from a function

What will be the output of the code segment?


Revisiting Pointers
Function and pointers
Returning multiple values from a function

What will be the output of the code segment?


Revisiting Pointers
Function and pointers

What will be the output of the code segment?


Revisiting Pointers
Function and pointers

What will be the output of the code segment?


Revisiting Pointers
Function and pointers

What will be the output of the code segment?


Revisiting Pointers
Function and pointers

What will be the output of the code segment?


Revisiting Pointers
Character array and pointer
What will be the output of the code segment?
Revisiting Pointers
Character array and pointer
What will be the output of the code segment?
Revisiting Pointers
Character array and pointer

What will be the output of the code segment?


Revisiting Pointers
Character array and pointer

What will be the output of the code segment?


Revisiting Pointers
Character array and pointer

What will be the output of the code segment?


Revisiting Pointers
Character array and pointer

What will be the output of the code segment?


Revisiting Pointers (and arrays)
What will be the output of the code segment?
Revisiting Pointers (and arrays)
What will be the output of the code segment?
Revisiting Pointers (and arrays)
What will be the output of the code segment?
Revisiting Pointers (and arrays)
What will be the output of the code segment?
Revisiting Pointers (and structures)
Pointer and structures

struct class {
int roll;
char name[20];
float marks;
};

struct class *ptr;

Once ptr points to a structure variable, the members can be


accessed as:

ptr –> roll; Arrow operator used to access members of a structure,


ptr –> name; through a structure-type pointer.
ptr –> marks;
ptr -> member is a shortcut for (*ptr).member.
Revisiting Pointers (and structures)
When using structure pointers, we should take care of operator precedence.

Member operator “.” has higher precedence than the dereferencing operator “*”

ptr –> roll and (*ptr).roll mean the same thing.

*ptr.roll will lead to error.

The operator “–>” enjoys the highest priority among operators.

++ptr –> roll will increment roll, not ptr.

(++ptr) –> roll will increment the pointer (to the next structure in an array) and
access roll in the next structure.
Revisiting Pointers (and structures)
What will be the output of the code segment?
Revisiting Pointers
Practice Problems

1. Extend the complex number program to include functions for addition, subtraction, multiplication, and
division

2. Define a structure for representing a point in two-dimensional Cartesian coordinate system. Using this
structure for a point, do the following.

(a) Write a function to return the distance between two given points
(b) Write a function to return the middle point of the line segment joining two given points
(c) Write a function to compute the area of a triangle formed by three given points

You might also like