When you pass a char-pointer to cout, it prints out the char being pointed to and every value in every subsequent memory location until it finds the value zero.
&test[0]
This is a char-pointer, pointing to the first char in the array, so cout outputs that char and every char following it, until it finds a zero (which in this case, is right after the final space you've got there).
test
This is a char-pointer, pointing to the first char in the array, so cout outputs that char and every char following it, until it finds a zero (which in this case, is right after the final space you've got there).
You're passing two identical char-pointers, so you get identical results.
If you want it to print out the address of the zeroth character, you need to pass it something that ISN'T a char-pointer. If you pass cout a void-pointer, cout has been programmed to output the value of that pointer - NOT what it's pointing at. When you pass cout a char-potiner, cout HAS been programmed to output what that pointer is pointing at.
What exactly is happening behind the scenes here? |
Behind the scenes, the function that gets called when you pass cout a char-pointer is different to the function that gets called when you pass cout a void-pointer. Different code is executed.