Chapter 3.2 CPP 23 - 24
Chapter 3.2 CPP 23 - 24
Chapter 3.2 CPP 23 - 24
Syntax :
data_type array_name[array_size];
a. Initializing at the time of declaration: You can set initial values for the array
when declaring it.
Example:
int numbers[3] = {1, 2, 3};
b. Partial initialization: You can partially initialize an array, and the remaining
elements will be set to default values (usually 0 for numeric types).
Example:
int data[5] = {1, 2}; // Initializes the first two elements to 1 and 2; the rest are
set to 0.
c. Initializing with a loop: This is useful for populating an array with values
calculated at runtime.
Example:
int squares[5];
for (int i = 0; i < 5; i++)
{ squares[i] = i * i;}
Array Name as a Pointer:
In C and C++, the name of an array is essentially a pointer to the first element of
the array. When you use the array name in an expression without an index, it
acts as a pointer to the first element.
Example:
int numbers[5] = {1, 2, 3, 4, 5};
int *ptr = numbers; // Assigns the address of the first element to the pointer.
Array Indexing:
You can access individual elements of an array using square brackets and an
index. This is equivalent to pointer arithmetic.
Example:
int secondElement = numbers[1]; // Accessing the second element using array
indexing. int thirdElement = *(numbers + 2); // Accessing the third element using
pointer arithmetic.
Pointer Arithmetic:
Pointers can be used to traverse an array by incrementing or
decrementing the pointer. This is more flexible and powerful than
array indexing.
Example:
int *ptr = numbers;
int thirdElement = *(ptr + 2); // Accessing the third element using
pointer
Example:
int main()
{ int myArray[] = {1, 2, 3, 4, 5};
int size = 5;
printArray(myArray, size);
return 0;
}
Pass by Reference:
You can also pass an array by reference. This allows the function to
work with the original array without creating a copy.
Example:
void modifyArray(int (&arr)[5])
{ for (int i = 0; i < 5; i++)
arr[i] *= 2; }
int main()
{ int myArray[] = {1, 2, 3, 4, 5};
modifyArray(myArray);
for (int i = 0; i < 5; i++)
{ cout << myArray[i] << " "; }
cout << endl;
return 0; }
Pass by std::array or std::vector: (Works on C++11)
In modern C++, it's often recommended to use std::array or std::vector from the
Standard Library instead of built-in arrays when passing arrays to functions. These
container classes offer safer and more convenient ways to work with arrays.
#include <iostream>
#include <array>
#include <vector>
int main()
{ std::vector<int> myVector = {1, 2, 3, 4, 5};
printVector(myVector);
return 0;}
Pass by a dynamic array (using pointers):
When working with dynamically allocated arrays (created with new
in C++), you can pass them to functions as pointers. Be cautious
when using dynamic arrays to ensure proper memory management
(e.g., using delete when you're done with the array).
int main()
{ int* dynamicArray = new int[5];
// Initialize dynamicArray
processDynamicArray(dynamicArray, 5);
return 0;}
namespace
• In C++, a namespace is a mechanism that helps prevent
naming conflicts and organizes code into logical groups.
int main()
{ // Accessing variables and functions from a namespace
int x = my_namespace::my_variable;
my_namespace::my_function();
return 0;
}