I thought I had a decent knowledge of pointers until I decided to review some so called trivial examples.
One thing I know is that when declaring an array say:
int arr[2] {3, 5};
arr will hold the value of the first element in the array so attempting to print that (cout << arr) gives obviously the address of arr[0]. Even thought my program uses pointers it's still similar.
My question is why can I print h and have bonjour as output, but I can't do the same with p?
It also looks like when I increment h++ and print it again I get onjour. How are pointers different with char?
#include <iostream>
#include <string>
int main()
{
char* h = "bonjour";
int k[4]{3, 4, 5, 6};
int * p = k;
std::cout << "Hello, "<< h << "!\n";
}
const char *that does it for you. Practically no one ever wants to output an array of int without some special formatting, and even then how would << know when to stop? With a char array, 0 terminates the string, but 0 is just another number in an int array.