Loading...

Go Back

Next page
Go Back Course Outline

C++ Full Course


Arrays in C++

Arrays in C++

Arrays in C++ are contiguous blocks of memory that hold elements of the same data type. They provide a way to store and access multiple values using a single variable name and an index.

Static Arrays

Static arrays have a fixed size that is determined at compile time. The size must be a constant expression.

Declaration and Initialization

Here are several ways to declare and initialize static arrays in C++:

Declaration without Initialization

#include <iostream>
int main() { int numbers[5]; // Declares an integer array named 'numbers' that can hold 5 elements // You can then assign values to the elements using their index (0-based) numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; std::cout << "Element at index 2: " << numbers[2] << std::endl; return 0; }
Element at index 2: 30

Initialization with a List of Values

#include <iostream>

int main() {
  int scores[3] = {85, 92, 78}; // Declares and initializes an integer array with 3 elements

  std::cout << "Score at index 1: " << scores[1] << std::endl;

  return 0;
}
Score at index 1: 92


Initialization with Partial Values

If you provide fewer initializers than the size of the array, the remaining elements are default-initialized (usually to 0 for numeric types).

#include <iostream>

int main() {
  double values[5] = {3.14, 2.71}; // First two elements are initialized, the rest are 0.0

  std::cout << "Value at index 0: " << values[0] << std::endl;
  std::cout << "Value at index 3: " << values[3] << std::endl;

  return 0;
}
Value at index 0: 3.14
Value at index 3: 0

Initialization without Explicit Size

If you provide an initialization list, you can omit the size of the array. The compiler will deduce the size based on the number of initializers.

#include <iostream>

int main() {
  char message[] = {'H', 'e', 'l', 'l', 'o'}; // Size is automatically 5
  std::cout << "Message: ";
  for (char c : message) {
    std::cout << c;
  }
  std::cout << std::endl;

  int numbers[] = {100, 200, 300}; // Size is automatically 3
  std::cout << "Number at index 2: " << numbers[2] << std::endl;

  return 0;
}
Message: Hello
Number at index 2: 300

Accessing Array Elements

You access individual elements of an array using their index, which starts from 0 for the first element and goes up to size - 1 for the last element.

#include <iostream>

int main() {
  int data[] = {5, 10, 15, 20};

  std::cout << "First element: " << data[0] << std::endl;
  std::cout << "Second element: " << data[1] << std::endl;
  std::cout << "Last element: " << data[3] << std::endl;

  // Be careful not to access elements outside the array bounds (e.g., data[4] in this case),
  // as it leads to undefined behavior.

  return 0;
}
First element: 5
Second element: 10
Last element: 20


Dynamic Arrays

Dynamic arrays have their size determined at runtime. They are created using dynamic memory allocation (new and delete[]).

Creating Dynamic Arrays

#include <iostream>

int main() {
  int size;
  std::cout << "Enter the size of the array: ";
  std::cin >> size;

  int* dynamicArray = new int[size]; // Allocate an integer array of the specified size on the heap

  // Initialize the elements
  for (int i = 0; i < size; ++i) {
    dynamicArray[i] = i * 10;
  }

  // Access and print the elements
  std::cout << "Elements of the dynamic array: ";
  for (int i = 0; i < size; ++i) {
    std::cout << dynamicArray[i] << " ";
  }
  std::cout << std::endl;

  delete[] dynamicArray; // Deallocate the dynamically allocated memory
  dynamicArray = nullptr; // Good practice to set the pointer to nullptr

  return 0;
}
Enter the size of the array: 5
Elements of the dynamic array: 0 10 20 30 40

Key Points about Dynamic Arrays

  • The size of a dynamic array is not fixed at compile time.
  • Memory for dynamic arrays is allocated on the heap using the new[] operator.
  • You must explicitly deallocate the memory of a dynamic array using the delete[] operator when it's no longer needed to prevent memory leaks.
  • Dynamic arrays are often used when the size of the array is not known until the program is running.
Go Back

Next page