Hello, i was trying to learn about pointers and i came across this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
using namespace std;
int main()
{
char *p;
int i=0;
p= new char [10];
cin>>p;
while (i<10)
{
cin.clear();
cout <<"the p is: "<<*p++<<endl;
i++;
}
return 0;
}
|
i have few question,when i allocated the dynamic char, for the ponters, the pointer "p" points to first block of char. thus printing *p, should print char [0]. but i have a problem if i say cout <<p; it prints the whole entire char array, and if i do this p++, it prints the array from 1-9, if i do p++ again it would print array from 2-9. i don't understand this.
using *p works fine, since it de referencing, thus value pointed by p. also *p++ works perfectly, first run would print char [0], then [1], etc.
can you explain to me whats going on, and the difference with printing p and *p.
Also final question, if i do cin >>p; it works fine although i don't understand it because i am not reading in to p, but rather the value being pointed by p. thus this should be valid cin>>*p; but this doesn't read in properly, this reads in rubbish.