SlideShare a Scribd company logo
Placement Preparation 
Arrays & Pointers 
Shobhit Chaurasia 
B.Tech, CSE
Target Audience 
People 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.
Data types 
● int : -1000, -5, 0, 10, 2014 
● char : a, @, + 
● float : -10.35, 2.578 
● bool : True/False
More Data types 
● Array - {1,2,3,4} 
● String - “Ghissu” 
● Pointers
Arrays 
● Array is a collection of objects.
Arrays - 1D 
● int test[10]; // space for 10 ints 
● 0-based indexing 
● Index from 0 to (size - 1)
Array - initialization 
1D 
2D
Demo #1 
Store first 10 +ve integers in an array and 
print them.
Demo #2 
Sum up all the numbers in an array.
Demo #3 
Find the max. number in an array.
Demo #4 
Reverse the contents of an array. 
int arr[10]={1,2,3,4,5,6,7,8,9,10};
Arrays - 2D (Matrix) 
● int test_score[x][y] 
Row Index 
Column Index 
int test_score[10][4]
Demo #5 
Output contents of a 2D int array.
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
Arrays - 3D
Address of Variables 
● int x = 10; //value of x is 10 
● address of variable x is &x 
x 
10 
0x7fff4d84231c 
Variable Name 
Data/Content 
Address of variable
Demo #7 
Print the value and address of a float
Pointers 
Pointer is a variable that contains memory 
address. 
ptr 
0x7fff4d84231c 
p 
0x7fff01f8239c 
f 
0x00abcd12209
ptr 
0x7fff4d84231c 
p 
f 
0x243f01f8239a 0x00abcd12209 
10 
0x7fff4d84231c 
s 
0x243f01f8239a 
0.3 
0x00abcd12209
Lecture 2: arrays and pointers
Demo #8 
&x returns the 
address of 
variable x
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 
6 
0x7fff4d84231c 
ptr 
x
Pointer Dereferencing 
*ptr returns the value stored at the memory 
location pointed to by ptr
Demo #9 
➔ Show and explain 9.cpp 
➔ Ask them to reproduce the code without looking
NULL Pointer 
int *ptr = NULL; //good coding practice 
ptr
Pointer to Pointer 
int x = 10; 
int *p = &x; 
int **q = &p; 
q 
0x32 
0x74 
p 
0x66 
0x32 
x 
10 
0x66
Arrays as pointers 
int arr[7]; 
Array Index 
Data 
arr 
0 1 2 3 4 5 6 
- 22 - 33 - 44 - 55 - 66 - 77 - 88 
0014 0018 0022 0026 0030 0034 0038 
arr + 1 arr +2 arr + 6 
Address
0 1 2 3 4 5 6 
- 22 - 33 - 44 - 55 - 66 - 77 - 88 
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
To be continued ... 
● Dynamic memory allocation 
● C-style strings. 
● strcmp, strcat etc.

More Related Content

PPTX
CSE240 Pointers
PPT
Arrays
PDF
Data structure week 2
PPT
Pointers in c
PDF
Array notes
DOC
C tech questions
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PPT
Pointers
CSE240 Pointers
Arrays
Data structure week 2
Pointers in c
Array notes
C tech questions
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Pointers

What's hot (20)

PPT
detailed information about Pointers in c language
PDF
Data structure week 3
PPTX
C++ examples &revisions
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
PPT
Pointers+(2)
PPS
pointers 1
PPTX
C++ Pointers
PPT
Pointers in C
PPT
Fp201 unit4
PPTX
Learn Function The Hard Way
PDF
C++ ARRAY WITH EXAMPLES
PPTX
C sharp 8
PPT
FP 201 - Unit4 Part 2
DOCX
C interview question answer 2
PDF
OOP 2012 - Hint: Dynamic allocation in c++
PPT
Lecture#8 introduction to array with examples c++
PPTX
C++ programming pattern
PPTX
detailed information about Pointers in c language
Data structure week 3
C++ examples &revisions
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Pointers+(2)
pointers 1
C++ Pointers
Pointers in C
Fp201 unit4
Learn Function The Hard Way
C++ ARRAY WITH EXAMPLES
C sharp 8
FP 201 - Unit4 Part 2
C interview question answer 2
OOP 2012 - Hint: Dynamic allocation in c++
Lecture#8 introduction to array with examples c++
C++ programming pattern
Ad

Similar to Lecture 2: arrays and pointers (20)

PPTX
C++ Pointer | Introduction to programming
PPTX
Pointers Notes.pptx
PPTX
POLITEKNIK MALAYSIA
PPSX
Pointers
PPTX
C programming - Pointer and DMA
PPTX
C Programming : Pointers and Arrays, Pointers and Strings
PPT
ch08.ppt
PPTX
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
PPTX
3.ArraysandPointers.pptx
PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PPTX
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
PPTX
ESC112 Course lecture slides on pointers and memory allocation.pptx
PPT
FP 201 - Unit 6
PPT
C pointers
PDF
C Programming - Refresher - Part III
PPT
Pointers definition syntax structure use
PPT
Chapter09-10 Pointers and operations .PPT
PPT
Chapter09-10.PPT
PPTX
Object Oriented Programming using C++: Ch10 Pointers.pptx
PPT
Pointer
C++ Pointer | Introduction to programming
Pointers Notes.pptx
POLITEKNIK MALAYSIA
Pointers
C programming - Pointer and DMA
C Programming : Pointers and Arrays, Pointers and Strings
ch08.ppt
Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx
3.ArraysandPointers.pptx
week14Pointers_II. pointers pemrograman dasar C++.pptx
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
ESC112 Course lecture slides on pointers and memory allocation.pptx
FP 201 - Unit 6
C pointers
C Programming - Refresher - Part III
Pointers definition syntax structure use
Chapter09-10 Pointers and operations .PPT
Chapter09-10.PPT
Object Oriented Programming using C++: Ch10 Pointers.pptx
Pointer
Ad

More from Vivek Bhargav (10)

PPTX
Lecture 11.2 : sorting
PPTX
Lecture 11.1 : heaps
PPTX
Lecture 10 : trees - 2
PPTX
Lecture 9: Binary tree basics
PPTX
Lecture 7 & 8: Stack & queue
PPTX
Lecture 6: linked list
PPTX
Lecture 5: Asymptotic analysis of algorithms
PPTX
Lecture 4: Functions
PPTX
Lecture 3: Strings and Dynamic Memory Allocation
PPTX
Lecture 1: basic syntax
Lecture 11.2 : sorting
Lecture 11.1 : heaps
Lecture 10 : trees - 2
Lecture 9: Binary tree basics
Lecture 7 & 8: Stack & queue
Lecture 6: linked list
Lecture 5: Asymptotic analysis of algorithms
Lecture 4: Functions
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 1: basic syntax

Recently uploaded (20)

PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Well-logging-methods_new................
PPTX
additive manufacturing of ss316l using mig welding
PPT
Total quality management ppt for engineering students
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
PPT
Project quality management in manufacturing
PPTX
Geodesy 1.pptx...............................................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
737-MAX_SRG.pdf student reference guides
PPTX
Fundamentals of Mechanical Engineering.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
Current and future trends in Computer Vision.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Well-logging-methods_new................
additive manufacturing of ss316l using mig welding
Total quality management ppt for engineering students
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Foundation to blockchain - A guide to Blockchain Tech
CYBER-CRIMES AND SECURITY A guide to understanding
Sustainable Sites - Green Building Construction
Project quality management in manufacturing
Geodesy 1.pptx...............................................
Embodied AI: Ushering in the Next Era of Intelligent Systems
737-MAX_SRG.pdf student reference guides
Fundamentals of Mechanical Engineering.pptx
Mechanical Engineering MATERIALS Selection
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Current and future trends in Computer Vision.pptx

Lecture 2: arrays and pointers

  • 1. Placement Preparation Arrays & Pointers Shobhit Chaurasia B.Tech, CSE
  • 2. Target Audience People 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.
  • 3. Data types ● int : -1000, -5, 0, 10, 2014 ● char : a, @, + ● float : -10.35, 2.578 ● bool : True/False
  • 4. More Data types ● Array - {1,2,3,4} ● String - “Ghissu” ● Pointers
  • 5. Arrays ● Array is a collection of objects.
  • 6. Arrays - 1D ● int test[10]; // space for 10 ints ● 0-based indexing ● Index from 0 to (size - 1)
  • 8. Demo #1 Store first 10 +ve integers in an array and print them.
  • 9. Demo #2 Sum up all the numbers in an array.
  • 10. Demo #3 Find the max. number in an array.
  • 11. Demo #4 Reverse the contents of an array. int arr[10]={1,2,3,4,5,6,7,8,9,10};
  • 12. Arrays - 2D (Matrix) ● int test_score[x][y] Row Index Column Index int test_score[10][4]
  • 13. Demo #5 Output contents of a 2D int array.
  • 14. 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
  • 16. Address of Variables ● int x = 10; //value of x is 10 ● address of variable x is &x x 10 0x7fff4d84231c Variable Name Data/Content Address of variable
  • 17. Demo #7 Print the value and address of a float
  • 18. Pointers Pointer is a variable that contains memory address. ptr 0x7fff4d84231c p 0x7fff01f8239c f 0x00abcd12209
  • 19. ptr 0x7fff4d84231c p f 0x243f01f8239a 0x00abcd12209 10 0x7fff4d84231c s 0x243f01f8239a 0.3 0x00abcd12209
  • 21. Demo #8 &x returns the address of variable x
  • 22. 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 6 0x7fff4d84231c ptr x
  • 23. Pointer Dereferencing *ptr returns the value stored at the memory location pointed to by ptr
  • 24. Demo #9 ➔ Show and explain 9.cpp ➔ Ask them to reproduce the code without looking
  • 25. NULL Pointer int *ptr = NULL; //good coding practice ptr
  • 26. Pointer to Pointer int x = 10; int *p = &x; int **q = &p; q 0x32 0x74 p 0x66 0x32 x 10 0x66
  • 27. Arrays as pointers int arr[7]; Array Index Data arr 0 1 2 3 4 5 6 - 22 - 33 - 44 - 55 - 66 - 77 - 88 0014 0018 0022 0026 0030 0034 0038 arr + 1 arr +2 arr + 6 Address
  • 28. 0 1 2 3 4 5 6 - 22 - 33 - 44 - 55 - 66 - 77 - 88 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
  • 29. To be continued ... ● Dynamic memory allocation ● C-style strings. ● strcmp, strcat etc.