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
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:
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
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
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;
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.