Mastering C++: From Fundamentals to Advanced Programming

0 0 0 0 0

Chapter 5: Arrays, Strings, and Pointers in C++

🔹 1. Introduction

In this chapter, we’ll explore the core data manipulation tools in C++:

  • Arrays: Store multiple values of the same type in one variable.
  • Strings: Represent and manipulate sequences of characters.
  • Pointers: Directly access memory locations.

Together, they form the foundation of low-level programming, data structures, and memory optimization in C++.


🔹 2. Arrays in C++

What is an Array?

An array is a fixed-size collection of elements of the same data type, stored in contiguous memory.

Declaring and Initializing Arrays:

int nums[5];                  // Declaration

int scores[3] = {90, 80, 70}; // Initialization

Accessing Elements:

cout << scores[0];  // 90

Looping Through Arrays:

for (int i = 0; i < 3; i++) {

    cout << scores[i] << " ";

}


🔹 3. Multidimensional Arrays

Syntax:

int matrix[2][3] = {

  {1, 2, 3},

  {4, 5, 6}

};

Accessing Elements:

cout << matrix[1][2]; // 6

Nested Loop Example:

for (int i = 0; i < 2; i++) {

    for (int j = 0; j < 3; j++) {

        cout << matrix[i][j] << " ";

    }

    cout << endl;

}


🔹 4. Strings in C++

C++ supports both:

  • C-style strings (character arrays)
  • std::string class (from the Standard Library)

C-style String:

char name[6] = "Alice";

cout << name;

Common String Functions (C-style):

#include <cstring>

strlen(name);          // Length

strcpy(dest, src);     // Copy

strcmp(a, b);          // Compare


Using std::string:

#include <string>

string name = "Bob";

cout << name.length();  // 3

String Functions (std::string):

name.append(" Smith");

name.substr(0, 3);      // Substring

name.find("o");         // Index of 'o'

std::string is easier and safer to use than char arrays.


🔹 5. Introduction to Pointers

What is a Pointer?

A pointer stores the memory address of a variable.

Declaring and Using Pointers:

int x = 10;

int *ptr = &x;

 

cout << *ptr;   // 10 (value)

cout << ptr;    // address

Pointer Arithmetic:

ptr++; // Moves to the next memory location

Use * to dereference (access value), & to get address.


🔹 6. Pointers and Arrays

int arr[] = {1, 2, 3};

int *p = arr;

 

cout << *p;     // 1

cout << *(p+1); // 2

Arrays and pointers are tightly related — the array name acts as a pointer to the first element.


🔹 7. Pointers with Functions

void update(int *n) {

    *n += 5;

}

 

int main() {

    int a = 10;

    update(&a);

    cout << a;  // 15

}

Pointers allow functions to modify arguments directly.


🔹 8. Summary Table

Concept

Syntax Example

Array declaration

int a[5];

2D Array

int b[3][3];

String (C-style)

char name[] = "Ali";

String (C++)

string name = "Ali";

Pointer

int *ptr = &x;

Dereference

*ptr

Address

&x



Back

FAQs


1. What is C++ used for today?

C++ is used in game development, operating systems, compilers, IoT, robotics, and performance-critical applications

2. Is C++ beginner-friendly?

  1. Yes — it’s a great first language, though it requires understanding of memory management and syntax.

3. What’s the difference between C and C++?

C is procedural; C++ supports both procedural and object-oriented programming

4. How is memory managed in C++?

Using pointers, references, and manual memory control with new and delete.

5. What is the STL in C++?

The Standard Template Library is a powerful collection of classes like vector, set, map, and queue.

6. Is C++ still relevant in 2025 and beyond?

Absolutely. It's critical in industries like gaming, defense, automotive, and finance

7. Can I build web apps with C++?

Not commonly — C++ is not typically used for frontend or backend web apps. However, it powers backend engines in browsers.

8. Is C++ used in machine learning?

Yes, many machine learning libraries (like TensorFlow) have C++ backends for performance.

9. How hard is it to switch from C++ to Python?

Very easy — once you’ve mastered C++, most modern languages feel simpler.