C++ – Arrays and pointers Flashcards

(9 cards)

1
Q

Array

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Iterating Example

A
#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;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Element-wise Operations

A

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;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

C-Style Strings vs std::string

A

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;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Pointers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Modifying Data via Pointer

A

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;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Null Pointers

A

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; 
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Pointers and Arrays

A

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;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Pointer Arithmetic

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly