|
From: JYL <eji...@gm...> - 2009-03-23 07:49:53
|
Hi, everyone
I have a question about pointer in C++, below is my source code and
question, if anyone can answer my question, I will very appreciate, thanks a
lot
//source code and question start here
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
int num[]={11,22,33,44,55};
char name[6]={'j','i','a','n','y','i'};
char *ptr="How are you?";
cout << "int num[]={11,22,33,44,55}" << endl;
cout << "num=" << num << endl;
cout << "&num=" << &num << endl;
cout << "*num=" << *num << endl;
cout<< endl << "char name[6]={'j','i','a','n','y','i'};" << endl;
cout << "name=" << name << endl;
/*here,
question 1: why output name is all of the array content,
not the address as same as output of &name??
the array name is a pointer, doesn't the content of array name is the
same as the address of array name
question 2: why there is a random character after
output of i??
*/
cout << "&name=" << &name << endl;
cout << "*name=" << *name << endl;
cout << endl << "char *ptr=\"How are you?\";" << endl;
cout << "ptr=" << ptr << endl;
/* here, the same of question 1*/
cout << "&ptr=" << &ptr << endl;
cout << "*ptr=" << *ptr << endl;
ptr=ptr+1;
cout << endl << "ptr=ptr+1;" << endl;
cout << "ptr=" << ptr << endl;
cout << "&ptr=" << &ptr << endl;
/* here, why the address of ptr doesn't change??*/
cout << "*ptr=" << *ptr << endl;
system("pause");
return 0;
}
|