CPP Best Practices and Notes -- Part 9

Notes taken from https://www.learncpp.com/

9.2 Arrays II

  • Explicitly initialize arrays, even if they would be initialized without an initializer list.

  • When passing arrays to functions, C++ does not copy an array. The actual array is passed.

    • To ensure a function does not modify the array passed into it, make the array const:
      1
      2
      3
      void functionWithArray(const int arr[5]) {
      std::size(arr); // note that this will cause error!!
      }
  • When you are in the same function that a array is
    declared in, sizeof() will return the total size of the array (array length multiplied by element size)

    • When sizeof is used on an array that has been passed to a function, it doesn’t error
      out like std::size() does. Instead, it returns the size of a pointer.
Read more