#include <iostream>
usingnamespace std;
int main()
{
char *p="i am a string"; //What is it? A pointer variable which can hold an address of a char..isn't it?
cout<<p<<endl; //This prints i am a string..why?
cout<<*p<<endl; //This prints i why?
return 0;
}
Array names are basically pointers to the first element in an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
char *p = "i am a string"; //char array
cout << p << endl; //cout char array
cout << *p << endl; //This points to the first element in the char array
cout << strlen(p) << endl;//Length of array
return 0;
}
"i am a string" is an array of 14 char and p is a pointer to the first char in that array.
cout << p << endl;
The << operator has been overloaded to handle char pointers different from other pointer types because it's very common that char pointers are used for strings (especially in C).
cout<<*p<<endl;
p points to the first char in the array so *p returns that char and that's why it prints 'i'.