Array
Data structure that stores a fixed-size sequential collection of elements of the same type.
To declare an array, you specify the type of the elements and the number of elements the array will hold.
type arrayName[arraySize];
int numbers[5];
You can initialize an array at the time of declaration.
int numbers[5] = {10, 20, 30, 40, 50};
You can access individual elements of an array using their index. Array indices start at 0.
int thirdNumber = numbers[2]; // thirdNumber will be 30
Iterating Example
#include <iostream>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}Element-wise Operations
Common for tasks like applying calibration constants or scaling sensor data. This involves creating a new array where each element is the product of the corresponding elements from two other arrays.
// Multiply two arrays element by element
#include <iostream>
int main() {
const int SIZE = 5;
// Raw sensor readings
int readings[SIZE] = {101, 120, 95, 210, 150};
// Calibration factors
double factors[SIZE] = {0.5, 0.48, 0.51, 0.49, 0.5};
// Calibrated results
double calibrated[SIZE];
for (int i = 0; i < SIZE; ++i) {
calibrated[i] = readings[i] * factors[i];
}
std::cout << "Calibrated Values: ";
for (int i = 0; i < SIZE; ++i) {
std::cout << calibrated[i] << " ";
}
return 0;
}C-Style Strings vs std::string
In C, strings are represented as arrays of characters, terminated by a null character (\0).
char greeting[] = "Hello";
It is generally recommended to use std::string over C-style strings.
#include <iostream>
#include <string>
int main() {
std::string greeting = "Hello, C++!";
std::cout << greeting;
return 0;
}Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are a powerful feature of C++, but they must be used with care.
To declare a pointer, you use the * operator (dereference operator). Is used to access the value stored at the memory address held by a pointer. You can think of it as “following the pointer” to its target.
type *pointerName;int *p;
(&) returns the memory address of a variable.
int myVar = 42;
int *p = &myVar;
It helps to think of memory as a series of mailboxes. Each has a unique address.
myVar is a “mailbox” at address 0x7ffee…. Its content is the integer 42.p is another “mailbox” at a different address. Its content is the address of myVar, not its value.
Modifying Data via Pointer
Dereferencing also allows you to change the value at the pointer’s address. This is a key feature.
#include <iostream>
int main() {
int myVar = 42;
int *p = &myVar;
std::cout << "Original value: " << myVar << std::endl; // 42
*p = 99; // "Go to the address p is pointing to, and set its value to 99"
std::cout << "New value: " << myVar << std::endl; // 99!
return 0;
}Null Pointers
Pointer that does not point to any memory address. It’s good practice to initialize pointers to nullptr if you don’t have a valid address for them yet. Dereferencing a null pointer will cause a crash!
int *p = nullptr;
if (p != nullptr) {
// It is safe to dereference p here
std::cout << *p;
}Pointers and Arrays
The name of an array is actually a pointer to the first element of the array.
#include <iostream>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int *p = numbers; // p points to numbers[0]
std::cout << *p; // Prints 10
return 0;
}Pointer Arithmetic
You can use arithmetic operators (like + and ++) to move a pointer. The compiler is smart: p++ moves the pointer forward by sizeof(type) bytes, not just one byte.
int numbers[5] = {10, 20, 30, 40, 50};
int *p = numbers; // p points to 10
p++; // p now points to 20
std::cout << *p << std::endl; // Prints 20
p = p + 2; // p now points to 40
std::cout << *p << std::endl; // Prints 40
// Accessing elements:
std::cout << *(numbers + 3); // Prints 40. Same as numbers[3]