str is a pointer, why not use str for input and output? Not *str.
p is a pointer, why use *p for input and output? Not p.
#include<iostream>
using namespace std;
int main()
{
char* str = new char[20];
cin>>str;
cout<<str<<endl;
delete []str;
int* p = new int[3];
cin>>*p;
cout<<*p<<endl;
delete []p;
return 0;
}
The operator overloads
<<and>>have special overloads forconst char*andchar*respectively, because those are null-terminated C-style strings. They are treated diifferently than other pointers/other arrays.Here's a little comparison of the semantics used:
means "read a null terminated string into an array, where
stris the pointer to the first element".means "Print a null terminated string, where
stris the pointer to the first element".However there are such semantics for other pointer types like
int*.wont work, there is no such thing as "reading an array of ints", or "reading a pointer", while
works and means "read a single integer and store it in the value of
p", that is, the first element in the array get's modified.means "print the value of the pointer", again, because there are no special semantics for
int*like "Print array of ints". On the other handmeanse "Print one integer, that is the value of
p", that is, the first element in the array get's printed.