a char is a single character. Example:
1 2 3
|
char c = 'A';
cout << c; // prints "A"
|
When cout is passed a single char, like the above, it will only print the single char.
Now, C++ also treats string literals like an array of chars / or a pointer to an array of chars.
So:
1 2 3 4
|
// note the 'const' here.
const char* ptr = "example";
cout << ptr; // prints "example";
|
This is kind of a special case as Athar mentioned. When given a const char*, cout will interpret that as a C-style string and print the entire string. The actual pointer points to the first character in the string (in this case, the first 'e' in example). The rest of the string can be "found" by incrementing the pointer, since the string is stored sequentially in memory.
That said... let's look at your first example:
1 2 3 4
|
const char* ptr1 = "Computer"; // note: added const here for good measure
cout << "*ptr1 = " << *ptr1 << endl << endl;
cout << "ptr1 = " << ptr1 << endl << endl;
|
Here, you're using the * operator to dereference a pointer. When you dereference a pointer, you get whatever the pointer is pointing to.
So if
ptr1
is a
const char*
, then that means that
*ptr1
is a
const char
(ie: it's a single character).
Since ptr1 points to the first character in the string ('C'), that is what is printed when you print *ptr1. You're giving just a single char to cout, so that's all it prints.
However when you pass the pointer to cout, it will print the whole string because of the special case noted above.
Really, this is easier to understand with a type other than char, because char's are treated special. Let's look at an int:
1 2 3 4 5
|
int foo = 5;
int* ptr = &foo; // ptr points to foo
cout << *ptr; // prints '5'
cout << ptr; // prints the address of foo
|
Here, *ptr is a single int. Since ptr points to foo, printing *ptr is the same as printing foo. You get the output of '5'. But when you print ptr, that's exactly what you're printing: the pointer. The address in memory at which foo is stored.
char pointers are the same idea, only instead of printing the address, they are treated specially and are printed as a string instead.
Now with your second example:
1 2 3 4 5
|
const char* ptr[] = {
"one",
"two",
"three"
}
|
Here, ptr isn't really a pointer. It's an array of pointers. Kind of like a
const char**
(ie a pointer to a pointer).
hyper-technically this isn't true, but it might help you understand it
Therefore, in order to get a single character, you'd need to dereference the pointer twice:
1 2 3
|
ptr; // <- a const char** (pointer to a pointer)
*ptr; // <- a const char* (pointer)
**ptr; // <- a const char (single char)
|
Now remember that the bracket operator [] does the exact same thing as the indirection operator *. They both dereference the pointer.
So another way to view the above:
1 2 3
|
ptr; // <- a const char**
ptr[0]; // <- a const char*
ptr[0][0]; // <- a const char
|
So when you do this:
ptr[0] is a pointer to a const char, so "one" is printed as you'd expect.