I have heard people saying arr is a pointer to the first element or first character. |
This is not true.
arr is an array. However, when you use it it will automatically
decay to a pointer to the first element in many situations so you can often use it as if it was a char*.
|
std::cout << arr << std::endl;
|
It's not actually arr that is being passed to the << operator here. Instead what happens is that arr decays to a pointer to the first element and that's what being passed to the << operator.
When printing a char* using cout<< it will assume it points to a character in a null-terminated array. It will step through each character until it finds a null character '\0'. If the array does not contain a null character it leads to
undefined behaviour (UB), i.e. no guarantees what will happen (what usually happens is that it just continues printing whatever garbage that is located after the array in memory until it finds a null character [zero byte] or crashes because it tries to access memory that it's not allowed to).
A string literal like
"abc"
gives you such a null-terminated char array that you can print without problem.
1 2 3 4 5 6 7
|
std::cout << "abc"; // OK, prints "abc"
char str[] = "abc";
std::cout << str; // OK, also prints "abc"
char str[] = {'a', 'b', 'c', '\0'}; // this has the same meaning as line 3
std::cout << str; // OK, also prints "abc"
|
But your array
arr is not null-terminated (it does not contain a null character) so trying to print it is technically UB, meaning it's not guaranteed to output "FC".
This does the same thing.
|
std::cout << arr+0 << std::endl;
|
arr decays to a pointer when + is used and then you add nothing to that pointer so it's still a pointer to the first element, and of course it's still UB.
Printing any pointer type other than char* will print the address that the pointer holds.
&arr
gives you a pointer to
arr. The type of that pointer is
char(*)[2]
(pointer to array of 2 chars). So when you print that pointer it'll print the address (i.e. the address where the first byte of the array
arr is stored in memory).