SlideShare a Scribd company logo
1 of 61
ARRAYS AND
POINTERS IN C++
Group 6
C++ ARRAYS
An array in C++ is a data structure that stores a fixed-size
sequential collection of elements of the same type.
Each element in the array is accessed by its numerical index,
which represents its position within the array.
O nce you make an array, it can't change size. It stays the same
size the whole time your program runs.
Everything in an array is put in order next to each other in the
computer's memory.
INTRODUCTION TO ARRAYS
SYNTAX
EXAMPLE
01 .
02.
DECLARATION
ACCESSING ELLEMENTS
03.
ONE DIMENSIONAL & MULTIDIMENTIONAL ARRAYS
04.
INITIALIZATION
05.
ARRAY SIZE
KEY POINTS
DECLARATION
Declaration involves specifying the type
and size of the array
SYNTAX
EXAMPLE
SYNTAX
EXAMPLE
int - type of element to be stored
x - name of the array
6 - size of the array
ACCESSING ELEMENTS
In C++, each element in an array is associated with a number.
The number is known as an array index. Individual elements
within an array are accessed using index notation ([]).
ACCESSING ELEMENTS
Consider this example:
Few Things to R emember:
The array indices start with 0. Meaning x[0] is the first element stored at index 0.
If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the last element.
Elements of an array have consecutive addresses.
INITIALIZATION
Arrays can be initialized during declaration using initializer
lists {}. Partial initialization is also allowed, with the remaining
elements initialized to zero or default values.
EXAMPLE
ANOTHER WAY:
Here, we have not mentioned the size of the array. In such cases, the compiler
automatically computes the size.
INITIALIZATION
In C++, if an array has a size n, we can store up to n number of
elements in the array. However, what will happen if we store less
than n number of elements.
ARRAY WITH EMPTY MEMBERS
EXAMPLE
ARRAY SIZE
The size of an array is fixed and
determined at compile time. It
represents the total number of elements
that the array can hold. The sizeof
operator can be used to determine the
size of an array in bytes, allowing for
calculations and memory management.
ARRAY SIZE
EXAMPLE OUTPUT
ONE DIMENSIONAL ARRAYS
A O ne-Dimensional Array in C++ programming is a
special type of variable that can store multiple
values of only a single data type such as int, float,
double, char, structure, pointer, etc. at a contagious
location in computer memory. Here contagious
location means at a fixed gap in computer memory.
A O ne-Dimensional Array is also known as 1D
Array.
ONE DIMENSIONAL ARRAYS
we can see that the name of the one
dimensional array is a and it can store 5 integer
numbers. Size of the array is 5. Index of the
array is 0, 1, 2, 3 and 4.
The first index is called Lower Bound, and the
last index is called an Upper Bound. Upper
Bound of a one dimensional is always Size – 1.
ONE DIMENSIONAL ARRAYS
ONE DIMENSIONAL ARRAYS
To store the number in each cell of the array we can use the following syntax.
EXAMPLE O UTPUT
We can access any number stored in a 1D array using the following syntax.
Example 1: Program to input 10 numbers in an array and display only the even numbers if present in the array.
STORE AND ACCESS THE NUMBERS IN A 1 D ARRAY USING LOOPS
Here, you can see that we have run a for loop 10
times to store the user's input in the array. After
that we have run another for loop 10 times to
access each number from the array and print only
the even numbers from it.
Example 2: Program to input 5 numbers in an array and print all the numbers from the backside of the array.
STORE AND ACCESS THE NUMBERS IN A 1 D ARRAY USING LOOPS
Here, you can see that we have run a for loop 5 times
to store the user's input in the array. After that we
have run another for loop in reverse order to print all
the numbers from the back side of the array.
MULTIDIMENSIONAL ARRAYS
C++ supports multidimensional arrays,
allowing arrays of arrays. This enables
the creation of structures with multiple
dimensions, such as matrices, grids, or
tables, where elements are accessed
using multiple indices.
MULTIDIMENSIONAL ARRAYS
Here, x is a two-dimensional array. It can hold a maximum of 12
elements. We can think of this array as a table with 3 rows and each
row has 4 columns as shown below.
MULTIDIMENSIONAL ARRAYS
Three-dimensional arrays also work in a similar way. This array x can
hold a maximum of 24 elements.
We can find out the total number of elements in the array simply by
multiplying its dimensions:
MULTIDIMENSIONAL ARRAYS
1. Initialization of two-dimentional array
WR O NG WAY
R IG HT WAY
MULTIDIMENSIONAL ARRAYS
2. Initialization of three-dimentional array
R IG HT WAY
WR O NG WAY
MULTIDIMENSIONAL ARRAYS
EXAMPLE 1 : 2D ARRAY OUTPUT
MULTIDIMENSIONAL ARRAYS
EXAMPLE 2: TAKING INPUT FOR 2D ARRAY OUTPUT
MULTIDIMENSIONAL ARRAYS
EXAMPLE 3: TAKING INPUT FOR 2D ARRAY OUTPUT
POINTER ARITHMETIC AND ARRAY DECAY
Arrays can decay into pointers to their
first elements when passed to functions
or assigned to pointers. This means that
the name of the array can be treated as
a pointer to its first element. Pointer
arithmetic involves performing
arithmetic operations on array pointers,
allowing efficient traversal and
manipulation of array elements.
C++ ARRAY OUT OF BOUNDS
If we declare an array of size 10, then
the array will contain elements from
index 0 to 9.
However, if we try to access the
element at index 10 or more than 10, it
will result in undefined behavior.
C++ POINTERS
Pointers in C++ are variables that hold memory addresses.
They are used to indirectly access and manipulate data stored
in memory
Pointers are declared with a specific data type and can be
initialized to point to the address of another variable or set to
nullptr to indicate they don't currently point to anything.
They play a crucial role in dynamic memory allocation, data
structures, and low-level memory manipulation.
INTRODUCTION TO POINTERS
SYNTAX
WHILE ASSIGNING IT THE ADDRESS OF A VARIABLE
01 .
02.
DECLARATION AND INITIALIZATION
ADRESS AND POINTERS
03. POINTER ARITHMETIC
04. TYPES OF POINTERS
05. POINTER TO POINTER
KEY POINTS
ADDRESS IN C++
If we have a variable var in our program, &var will give us its
address in the memory. For example,
EXAMPLE OUTPUT
DECLARATION AND INITIALIZATION
Pointers in C++ are variables that hold
memory addresses. Declaration involves
specifying the data type that the pointer
will point to, using the asterisk ‘*’ symbol.
Initialization can be done by assigning
the address of a variable using the
address-of operator (‘&’) or initializing it
with ‘nullptr’ to indicate a null pointer.
DECLARATION AND INITIALIZATION
Here is how we can declare pointers:
We can also declare pointers in the following way:
Let's take another example of declaring pointers.
Here, we have declared a pointer pointVar and a normal variable p.
ADRESS AND POINTERS
ASSIGNING ADDRESSES TO POINTERS
Here, 5 is assigned to the variable var. And the address of var is
assigned to the pointVar pointer with the code pointVar =
&var.
ADRESS AND POINTERS
GET THE VALUE FROM THE ADDRESS USING POINTERS
The address of var is assigned to
pointVar. We have used the
*pointVar to get the value stored
in that address.
When * is used with pointers, it's
called the dereference operator.
It operates on a pointer and gives
the value pointed by the address
stored in the pointer. That is,
*pointVar = var.
Note: In C++, pointVar and *pointVar are
completely different. We cannot do
something like *pointVar = &var;
EXAMPLE OUTPUT
ADRESS AND POINTERS
ADRESS AND POINTERS
CHANGING VALUE POINTED BY POINTERS
Here, pointVar and &var have the same address; the value of var will
also be changed when *pointVar is changed.
EXAMPLE OUTPUT
ADRESS AND POINTERS
ADRESS AND POINTERS
COMMON MISTAKES WHEN WORKING WITH POINTERS
Suppose we want a pointer varPoint to point to the address of var. Then,
POINTER ARITHMETIC
Pointer arithmetic means performing arithmetic operations on
pointers. It refers to the operations that are valid to perform on
pointers.
Following are the arithmetic operations valid on pointers in C++:
1. Incrementing and Decrementing Pointers
2. Addition of Constant to Pointers
3. Subtraction of Constant from Pointers
4. Subtraction of Two Pointers of the Same Type
5. Comparison of Pointers
POINTER ARITHMETIC
EXAMPLE: INCREMENT AND DECREMENT OUTPUT
DO NO T CO PY
POINTER ARITHMETIC
EXAMPLE: ADDITION OF CONSTANT TO POINTERS OUTPUT
DO NO T CO PY
POINTER ARITHMETIC
EXAMPLE: SUBTRACTION OF CONSTANT FROM POINTERS OUTPUT
DO NO T CO PY
POINTER ARITHMETIC
EXAMPLE: SUBTRACTION OF TWO POINTERS OF THE SAME TYPE OUTPUT
DO NO T CO PY
POINTER ARITHMETIC
EXAMPLE: COMPARISON OF POINTERS OUTPUT
DO NO T CO PY
POINTER TYPES
The types of pointers are not so important. There is only one type
of pointer mainly used on a large scale. That is the normal pointer.
Along with that, there are two other types of pointers in C++.
- Normal Pointer
- Void Pointer
- Null Pointer
POINTER TYPES
NO R MAL PO INTER S
- It is the most simple pointer declaration process. It is the
foundation on which different operations of Pointer are executed.
Syntax: data-type *pointer-name;
POINTER TYPES
VO ID PO INTER S
- It can be used in every data type. This means these pointers are
declared in void form. This helps to reduce the pointer usability in
the program. O nly one pointer can be used in different cases.
Syntax: void *pointer-name;
POINTER TYPES
NULL PO INTER S
- In this case, a pointer is declared. But it will have the initial value
as Null. This means a later value is going to be provided there. This
is also a pre-step of the normal pointers. Sometimes a normal
pointer is declared as a Null pointer. After that, the value will be
provided there.
POINTER TO POINTER (DOUBLE POINTER)
Double pointer is termed as the Pointer To Pointer
operation in C++. Many experts call it “Pointers Pointers”.
The simple definition is a pointer is pointing towards
another pointer that is again pointed to a variable.
G eneral Syntax: data-type **pointer-name;
POINTER TO POINTER (DOUBLE POINTER)
EXAMPLE OUTPUT
POINTER TO POINTER (DOUBLE POINTER)
EXAMPLE
OUTPUT
In C++, Pointers are variables that hold addresses of other
variables. Not only can a pointer store the address of a single
variable, it can also store the address of cells of an array.
Consider th
ARRAY AND POINTER RELATIONSHIP
POINT TO EVERY ARRAY ELEMENTS
access the elements using the single pointer
suppose if we have initialized ptr = &arr[2]; then
EXAMPLE 1 : C++ POINTERS AND ARRAYS
IN THE ABO VE PR O G R AM, WE FIR ST
SIMPLY PR INTED THE ADDR ESSES O F THE
AR R AY ELEMENTS WITHO UT USING THE
PO INTER VAR IABLE PTR .
THEN, WE USED THE PO INTER PTR TO
PO INT TO THE ADDR ESS O F A[0], PTR + 1
TO PO INT TO THE ADDR ESS O F A[1], AND
SO O N.
EXAMPLE 2: ARRAY NAME USED AS POINTER
c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx

More Related Content

Similar to c++ arrays and pointers grade 9 STEP curriculum.pptx

Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in Carshpreetkaur07
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptxFolkAdonis
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxAmanRai352102
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT AcademyThe IOT Academy
 
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Stringsnikshaikh786
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programmingnmahi96
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arraysenidcruz
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxRamakrishna Reddy Bijjam
 

Similar to c++ arrays and pointers grade 9 STEP curriculum.pptx (20)

Array assignment
Array assignmentArray assignment
Array assignment
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
R Programming
R ProgrammingR Programming
R Programming
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT Academy
 
Module 4- Arrays and Strings
Module 4- Arrays and StringsModule 4- Arrays and Strings
Module 4- Arrays and Strings
 
Co&al lecture-08
Co&al lecture-08Co&al lecture-08
Co&al lecture-08
 
DEMO.ppt
DEMO.pptDEMO.ppt
DEMO.ppt
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Pandas csv
Pandas csvPandas csv
Pandas csv
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Lab6: I/O and Arrays
Lab6: I/O and ArraysLab6: I/O and Arrays
Lab6: I/O and Arrays
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Pointers
PointersPointers
Pointers
 

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

c++ arrays and pointers grade 9 STEP curriculum.pptx

  • 3. An array in C++ is a data structure that stores a fixed-size sequential collection of elements of the same type. Each element in the array is accessed by its numerical index, which represents its position within the array. O nce you make an array, it can't change size. It stays the same size the whole time your program runs. Everything in an array is put in order next to each other in the computer's memory. INTRODUCTION TO ARRAYS
  • 5. 01 . 02. DECLARATION ACCESSING ELLEMENTS 03. ONE DIMENSIONAL & MULTIDIMENTIONAL ARRAYS 04. INITIALIZATION 05. ARRAY SIZE KEY POINTS
  • 6. DECLARATION Declaration involves specifying the type and size of the array SYNTAX EXAMPLE SYNTAX EXAMPLE int - type of element to be stored x - name of the array 6 - size of the array
  • 7. ACCESSING ELEMENTS In C++, each element in an array is associated with a number. The number is known as an array index. Individual elements within an array are accessed using index notation ([]).
  • 8. ACCESSING ELEMENTS Consider this example: Few Things to R emember: The array indices start with 0. Meaning x[0] is the first element stored at index 0. If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the last element. Elements of an array have consecutive addresses.
  • 9. INITIALIZATION Arrays can be initialized during declaration using initializer lists {}. Partial initialization is also allowed, with the remaining elements initialized to zero or default values. EXAMPLE ANOTHER WAY: Here, we have not mentioned the size of the array. In such cases, the compiler automatically computes the size.
  • 10. INITIALIZATION In C++, if an array has a size n, we can store up to n number of elements in the array. However, what will happen if we store less than n number of elements. ARRAY WITH EMPTY MEMBERS EXAMPLE
  • 11. ARRAY SIZE The size of an array is fixed and determined at compile time. It represents the total number of elements that the array can hold. The sizeof operator can be used to determine the size of an array in bytes, allowing for calculations and memory management.
  • 13. ONE DIMENSIONAL ARRAYS A O ne-Dimensional Array in C++ programming is a special type of variable that can store multiple values of only a single data type such as int, float, double, char, structure, pointer, etc. at a contagious location in computer memory. Here contagious location means at a fixed gap in computer memory. A O ne-Dimensional Array is also known as 1D Array.
  • 14. ONE DIMENSIONAL ARRAYS we can see that the name of the one dimensional array is a and it can store 5 integer numbers. Size of the array is 5. Index of the array is 0, 1, 2, 3 and 4. The first index is called Lower Bound, and the last index is called an Upper Bound. Upper Bound of a one dimensional is always Size – 1.
  • 16. ONE DIMENSIONAL ARRAYS To store the number in each cell of the array we can use the following syntax. EXAMPLE O UTPUT We can access any number stored in a 1D array using the following syntax.
  • 17. Example 1: Program to input 10 numbers in an array and display only the even numbers if present in the array. STORE AND ACCESS THE NUMBERS IN A 1 D ARRAY USING LOOPS Here, you can see that we have run a for loop 10 times to store the user's input in the array. After that we have run another for loop 10 times to access each number from the array and print only the even numbers from it.
  • 18. Example 2: Program to input 5 numbers in an array and print all the numbers from the backside of the array. STORE AND ACCESS THE NUMBERS IN A 1 D ARRAY USING LOOPS Here, you can see that we have run a for loop 5 times to store the user's input in the array. After that we have run another for loop in reverse order to print all the numbers from the back side of the array.
  • 19. MULTIDIMENSIONAL ARRAYS C++ supports multidimensional arrays, allowing arrays of arrays. This enables the creation of structures with multiple dimensions, such as matrices, grids, or tables, where elements are accessed using multiple indices.
  • 20. MULTIDIMENSIONAL ARRAYS Here, x is a two-dimensional array. It can hold a maximum of 12 elements. We can think of this array as a table with 3 rows and each row has 4 columns as shown below.
  • 21. MULTIDIMENSIONAL ARRAYS Three-dimensional arrays also work in a similar way. This array x can hold a maximum of 24 elements. We can find out the total number of elements in the array simply by multiplying its dimensions:
  • 22. MULTIDIMENSIONAL ARRAYS 1. Initialization of two-dimentional array WR O NG WAY R IG HT WAY
  • 23. MULTIDIMENSIONAL ARRAYS 2. Initialization of three-dimentional array R IG HT WAY WR O NG WAY
  • 25. MULTIDIMENSIONAL ARRAYS EXAMPLE 2: TAKING INPUT FOR 2D ARRAY OUTPUT
  • 26. MULTIDIMENSIONAL ARRAYS EXAMPLE 3: TAKING INPUT FOR 2D ARRAY OUTPUT
  • 27. POINTER ARITHMETIC AND ARRAY DECAY Arrays can decay into pointers to their first elements when passed to functions or assigned to pointers. This means that the name of the array can be treated as a pointer to its first element. Pointer arithmetic involves performing arithmetic operations on array pointers, allowing efficient traversal and manipulation of array elements.
  • 28. C++ ARRAY OUT OF BOUNDS If we declare an array of size 10, then the array will contain elements from index 0 to 9. However, if we try to access the element at index 10 or more than 10, it will result in undefined behavior.
  • 30. Pointers in C++ are variables that hold memory addresses. They are used to indirectly access and manipulate data stored in memory Pointers are declared with a specific data type and can be initialized to point to the address of another variable or set to nullptr to indicate they don't currently point to anything. They play a crucial role in dynamic memory allocation, data structures, and low-level memory manipulation. INTRODUCTION TO POINTERS
  • 31. SYNTAX WHILE ASSIGNING IT THE ADDRESS OF A VARIABLE
  • 32. 01 . 02. DECLARATION AND INITIALIZATION ADRESS AND POINTERS 03. POINTER ARITHMETIC 04. TYPES OF POINTERS 05. POINTER TO POINTER KEY POINTS
  • 33. ADDRESS IN C++ If we have a variable var in our program, &var will give us its address in the memory. For example, EXAMPLE OUTPUT
  • 34. DECLARATION AND INITIALIZATION Pointers in C++ are variables that hold memory addresses. Declaration involves specifying the data type that the pointer will point to, using the asterisk ‘*’ symbol. Initialization can be done by assigning the address of a variable using the address-of operator (‘&’) or initializing it with ‘nullptr’ to indicate a null pointer.
  • 35. DECLARATION AND INITIALIZATION Here is how we can declare pointers: We can also declare pointers in the following way: Let's take another example of declaring pointers. Here, we have declared a pointer pointVar and a normal variable p.
  • 36. ADRESS AND POINTERS ASSIGNING ADDRESSES TO POINTERS Here, 5 is assigned to the variable var. And the address of var is assigned to the pointVar pointer with the code pointVar = &var.
  • 37. ADRESS AND POINTERS GET THE VALUE FROM THE ADDRESS USING POINTERS The address of var is assigned to pointVar. We have used the *pointVar to get the value stored in that address. When * is used with pointers, it's called the dereference operator. It operates on a pointer and gives the value pointed by the address stored in the pointer. That is, *pointVar = var. Note: In C++, pointVar and *pointVar are completely different. We cannot do something like *pointVar = &var;
  • 39. ADRESS AND POINTERS CHANGING VALUE POINTED BY POINTERS Here, pointVar and &var have the same address; the value of var will also be changed when *pointVar is changed.
  • 41. ADRESS AND POINTERS COMMON MISTAKES WHEN WORKING WITH POINTERS Suppose we want a pointer varPoint to point to the address of var. Then,
  • 42. POINTER ARITHMETIC Pointer arithmetic means performing arithmetic operations on pointers. It refers to the operations that are valid to perform on pointers. Following are the arithmetic operations valid on pointers in C++: 1. Incrementing and Decrementing Pointers 2. Addition of Constant to Pointers 3. Subtraction of Constant from Pointers 4. Subtraction of Two Pointers of the Same Type 5. Comparison of Pointers
  • 43. POINTER ARITHMETIC EXAMPLE: INCREMENT AND DECREMENT OUTPUT DO NO T CO PY
  • 44. POINTER ARITHMETIC EXAMPLE: ADDITION OF CONSTANT TO POINTERS OUTPUT DO NO T CO PY
  • 45. POINTER ARITHMETIC EXAMPLE: SUBTRACTION OF CONSTANT FROM POINTERS OUTPUT DO NO T CO PY
  • 46. POINTER ARITHMETIC EXAMPLE: SUBTRACTION OF TWO POINTERS OF THE SAME TYPE OUTPUT DO NO T CO PY
  • 47. POINTER ARITHMETIC EXAMPLE: COMPARISON OF POINTERS OUTPUT DO NO T CO PY
  • 48. POINTER TYPES The types of pointers are not so important. There is only one type of pointer mainly used on a large scale. That is the normal pointer. Along with that, there are two other types of pointers in C++. - Normal Pointer - Void Pointer - Null Pointer
  • 49. POINTER TYPES NO R MAL PO INTER S - It is the most simple pointer declaration process. It is the foundation on which different operations of Pointer are executed. Syntax: data-type *pointer-name;
  • 50. POINTER TYPES VO ID PO INTER S - It can be used in every data type. This means these pointers are declared in void form. This helps to reduce the pointer usability in the program. O nly one pointer can be used in different cases. Syntax: void *pointer-name;
  • 51. POINTER TYPES NULL PO INTER S - In this case, a pointer is declared. But it will have the initial value as Null. This means a later value is going to be provided there. This is also a pre-step of the normal pointers. Sometimes a normal pointer is declared as a Null pointer. After that, the value will be provided there.
  • 52. POINTER TO POINTER (DOUBLE POINTER) Double pointer is termed as the Pointer To Pointer operation in C++. Many experts call it “Pointers Pointers”. The simple definition is a pointer is pointing towards another pointer that is again pointed to a variable. G eneral Syntax: data-type **pointer-name;
  • 53. POINTER TO POINTER (DOUBLE POINTER) EXAMPLE OUTPUT
  • 54. POINTER TO POINTER (DOUBLE POINTER) EXAMPLE OUTPUT
  • 55. In C++, Pointers are variables that hold addresses of other variables. Not only can a pointer store the address of a single variable, it can also store the address of cells of an array. Consider th ARRAY AND POINTER RELATIONSHIP
  • 56. POINT TO EVERY ARRAY ELEMENTS access the elements using the single pointer suppose if we have initialized ptr = &arr[2]; then
  • 57.
  • 58. EXAMPLE 1 : C++ POINTERS AND ARRAYS IN THE ABO VE PR O G R AM, WE FIR ST SIMPLY PR INTED THE ADDR ESSES O F THE AR R AY ELEMENTS WITHO UT USING THE PO INTER VAR IABLE PTR . THEN, WE USED THE PO INTER PTR TO PO INT TO THE ADDR ESS O F A[0], PTR + 1 TO PO INT TO THE ADDR ESS O F A[1], AND SO O N.
  • 59. EXAMPLE 2: ARRAY NAME USED AS POINTER