lecture 2: arrays and pointers
DESCRIPTION
This slide is part of course designed for placement preparation for students of IIT Guwahati.TRANSCRIPT
![Page 1: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/1.jpg)
Placement Preparation
Arrays & Pointers
Shobhit ChaurasiaB.Tech, CSE
![Page 2: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/2.jpg)
Target AudiencePeople who have no prior coding experience, didn’t take CS101 seriously but want to learn some basics of coding quickly.
If you know how write a program to sum up numbers in an array, and you DO NOT faint on seeing int ***ptr, then please don’t your waste time here; this tutorial is not for you.
![Page 3: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/3.jpg)
Data types
● int : -1000, -5, 0, 10, 2014 ● char : a, @, +● float : -10.35, 2.578● bool : True/False
![Page 4: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/4.jpg)
More Data types
● Array - {1,2,3,4}
● String - “Ghissu”● Pointers
![Page 5: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/5.jpg)
Arrays
● Array is a collection of objects.
![Page 6: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/6.jpg)
Arrays - 1D
● int test[10]; // space for 10 ints
● 0-based indexing
● Index from 0 to (size - 1)
![Page 7: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/7.jpg)
Array - initialization
1D
2D
![Page 8: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/8.jpg)
Demo #1
Store first 10 +ve integers in an array and print them.
![Page 9: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/9.jpg)
Demo #2
Sum up all the numbers in an array.
![Page 10: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/10.jpg)
Demo #3
Find the max. number in an array.
![Page 11: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/11.jpg)
Demo #4
Reverse the contents of an array.
int arr[10]={1,2,3,4,5,6,7,8,9,10};
![Page 12: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/12.jpg)
Arrays - 2D (Matrix)
● int test_score[x][y]
Row Index
Column Index
int test_score[10][4]
![Page 13: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/13.jpg)
Demo #5
Output contents of a 2D int array.
![Page 14: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/14.jpg)
Demo #6
char mat[6][3];
0 1 2
0 e o e
1 o e o
2 e o e
3 o e o
4 e o e
5 o e o
![Page 15: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/15.jpg)
Arrays - 3D
![Page 16: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/16.jpg)
Address of Variables
● int x = 10; //value of x is 10
● address of variable x is &x
10
x
0x7fff4d84231c
Variable Name
Data/Content
Address of variable
![Page 17: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/17.jpg)
Demo #7
Print the value and address of a float
![Page 18: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/18.jpg)
Pointers
Pointer is a variable that contains memory address.
ptr
0x7fff4d84231c
p
0x7fff01f8239c
f
0x00abcd12209
![Page 19: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/19.jpg)
ptr
0x7fff4d84231c
p
0x243f01f8239a 0x00abcd12209
f
100x7fff4d84231c
s0x243f01f8239a
0.30x00abcd12209
![Page 20: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/20.jpg)
![Page 21: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/21.jpg)
Demo #8
&x returns the address of variable x
![Page 22: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/22.jpg)
Pointer Dereferencing
int *ptr;ptr = &x;cout<<ptr;cout<<*ptr;
Pointer Declaration
Making ptr point to x (x MUST be int)
Will print address of x
Will print the value stored at x
60x7fff4d84231c
ptrx
![Page 23: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/23.jpg)
Pointer Dereferencing
*ptr returns the value stored at the memory location pointed to by ptr
![Page 24: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/24.jpg)
Demo #9➔ Show and explain 9.cpp➔ Ask them to reproduce the code without looking
![Page 25: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/25.jpg)
NULL Pointer
int *ptr = NULL; //good coding practice
ptr
![Page 26: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/26.jpg)
Pointer to Pointer
int x = 10;int *p = &x;int **q = &p;
0x32
q
0x74
0x66
p
0x32
10
x
0x66
![Page 27: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/27.jpg)
Arrays as pointers
int arr[7];
arr
- 22 - 33 - 44 - 55 - 66 - 77 - 88
0014 0018 0022 0026 0030 0034 0038
0 1 2 3 4 5 6
arr + 1 arr +2 arr + 6
Address
Array Index
Data
![Page 28: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/28.jpg)
- 22 - 33 - 44 - 55 - 66 - 77 - 88
0 1 2 3 4 5 6
0014 0018 0022 0026 0030 0034 0038
arr[0] is same as *(arr + 0) is same as -22&(arr[0]) is same as (arr + 0) is same as 0014
arr[1] is same as *(arr + 1) is same as -33&(arr[1]) is same as (arr + 1) is same as 0018
arr[5] is same as *(arr + 5) is same as -77&(arr[5]) is same as (arr + 5) is same as 0034
Array Index
int arr[7]
Address
arr
![Page 29: Lecture 2: arrays and pointers](https://reader036.vdocument.in/reader036/viewer/2022062319/55620a1ed8b42af2128b4790/html5/thumbnails/29.jpg)
To be continued ...
● Dynamic memory allocation
● C-style strings.
● strcmp, strcat etc.