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
3void functionWithArray(const int arr[5]) {
std::size(arr); // note that this will cause error!!
}
- To ensure a function does not modify the array passed into it, make the array const:
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 likestd::size()
does. Instead, it returns the size of a pointer.
- When