C++ Arrays and Strings | 1D, 2D, Multidimensional Arrays and String Handling


This complete tutorial on C++ Arrays and Strings explains how to store and manipulate collections of data in C++. It covers one-dimensional, two-dimensional, and multidimensional arrays, character arrays, string handling functions, and the C++ string class. The tutorial follows best coding practices and helps beginners write efficient and readable C++ programs.

Arrays and Strings – Complete Tutorial

1. Arrays in C++

An array is a collection of elements of the same data type stored in contiguous memory locations.

Syntax:


data_type array_name[size];

Example:


int numbers[5] = {1, 2, 3, 4, 5};

2. One-Dimensional Arrays

One-dimensional arrays store data in a linear form.

Example:


int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;

Accessing elements:


cout << arr[1]; // Output: 20

Best Practices:

  1. Avoid out-of-bounds access
  2. Use loops for processing arrays

3. Two-Dimensional Arrays

Two-dimensional arrays are used to store data in rows and columns.

Syntax:


data_type array_name[rows][columns];

Example:


int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

Accessing elements:


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

4. Multidimensional Arrays

Arrays with more than two dimensions are called multidimensional arrays.

Example:


int cube[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};

Use Cases:

  1. 3D graphics
  2. Scientific computations

5. Character Arrays

Character arrays are used to store strings as a sequence of characters.

Example:


char name[10] = "Cplusplus";

Accessing characters:


cout << name[0];

Limitations:

  1. Fixed size
  2. No built-in functions for manipulation
  3. Risk of buffer overflow

6. String Handling Functions

C-style string functions are available in <cstring>.

Common functions:

  1. strlen()
  2. strcpy()
  3. strcat()
  4. strcmp()

Example:


#include <cstring>

char s1[20] = "Hello";
char s2[20] = "World";

strcat(s1, s2);
cout << s1;

Best Practices:

  1. Avoid C-style strings when possible
  2. Use safe functions and proper buffer sizes

7. string Class in C++

The string class (from <string>) provides a safer and more flexible way to handle strings.

Example:


#include <string>

string name = "C++ Programming";
cout << name;

Common Operations:


name.length();
name.append(" Language");
name.substr(0, 3);

Advantages over Character Arrays:

  1. Dynamic size
  2. Built-in functions
  3. Safer and easier to use

Character Array vs string Class

FeatureCharacter Arraystring Class
SizeFixedDynamic
SafetyLess safeSafer
FunctionsLimitedRich set
Ease of useComplexSimple

Best Practices for Arrays and Strings

  1. Prefer string over character arrays
  2. Always check array bounds
  3. Use loops efficiently
  4. Avoid hard-coded sizes when possible

Common Mistakes to Avoid

  1. Array index out of bounds
  2. Using uninitialized arrays
  3. Buffer overflow in character arrays
  4. Mixing C-style strings with string improperly

Summary

In this chapter, you learned how arrays and strings work in C++, including one-dimensional, two-dimensional, and multidimensional arrays, character arrays, string handling functions, and the string class. These concepts are fundamental for data storage and manipulation in C++ programs.