CodeTheRobot

C++ Beginner · Lesson 09 of 22

Arrays and strings: raw arrays, std::string, std::array

Fixed-size arrays, why raw arrays are risky, and safe alternatives.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain arrays and strings: raw arrays, std::string, std::array in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A raw array (double angles[3]) is a fixed block with no size stored with it; it silently decays to a pointer when passed to a function, and going past the end is undefined behaviour. std::array<T, N> has the same fixed size but knows its size() and works with the standard algorithms.

std::string owns and grows its text, and supports +, size() and comparison. Prefer std::string over char arrays. std::vector (Phase 3) is the resizable counterpart of std::array.

Guided lesson

Work through these steps

  1. 1.

    Read the arrays and strings: raw arrays, std::string, std::array example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

#include <array>
// Header for std::array.
#include <string>
// Header for std::string.
double raw[3] = {0.0, 0.5, -0.2};
// A raw array: three numbers, no bounds checking.
std::array<double, 3> joints{0.0, 0.5, -0.2};
// A safe fixed-size array that knows it holds 3 values.
joints.at(2) = 0.1;
// at() checks the index and throws if it is out of range.
std::string name{"shoulder"};
// A string that owns its characters.
name += "_joint";
// Append text; the string grows by itself.
std::cout << name << ' ' << name.size() << '\n';
// Print the text and its length (14).
for (double angle : joints) std::cout << angle << ' ';
// Range-based for visits each element.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Values in memory

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

motor speed030x1070x1410x1890x1c40x2060x2420x2880x2c*p += 10VALUES IN MEMORY

Demo for Arrays and strings: raw arrays, std::string, std::array. Expected result: shoulder_joint 14 0.6

Practice code

Now type it yourself

Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice

#include <array>
#include <iostream>
#include <string>

int main() {
  std::array<double, 3> joints{0.1, 0.2, 0.3};
  std::string name{"shoulder"};
  // TODO: append "_joint" to name
  double total{0.0};
  // TODO: add every joint angle to total with a range-based for loop
  std::cout << name << ' ' << name.size() << '\n' << total << '\n';
  return 0;
}

Expected output

shoulder_joint 14
0.6
Stuck? Show the full solution (try it yourself first)
#include <array>
#include <iostream>
#include <string>

int main() {
  std::array<double, 3> joints{0.1, 0.2, 0.3};
  std::string name{"shoulder"};
  name += "_joint";
  double total{0.0};
  for (double angle : joints) total += angle;
  std::cout << name << ' ' << name.size() << '\n' << total << '\n';
  return 0;
}

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Store six joint names and six angles in two std::array objects, print each pair, and reject a sixth-plus-one access with at(). Explain in one sentence why the raw-array version would not catch the mistake.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.