1.指针的定义 2.指针的使用 如下代码:
int a = 10;
int* p;
p = &a;
int* p=&a;
cout << "a的地址(&a):" << (int) & a << endl;
cout << "a的地址(p):" << (int)p << endl;
cout << "*p=" << *p << endl;
*p = 100;
cout << "*p=" << *p << endl;
cout << "a=" << a << endl;
运行结果: 3.指针所占内存空间
int a = 10;
int* p=&a;
cout << "sizeof(int *):" << sizeof(int *) << endl;
cout << "sizeof(p):" << sizeof(p) << endl;
cout << "sizeof(*p):" << sizeof(*p) << endl;
cout << "sizeof(char *):" << sizeof(char *) << endl;
cout << "sizeof(float *):" << sizeof(float *) << endl;
cout << "sizeof(double *):" << sizeof(double *) << endl;
cout << "sizeof(string *):" << sizeof(string *) << endl;
运行结果: 64位操作系统(x64) 32位操作系统(x84)
原因:指针所占内存空间大小与操作系统有关,32位4,64位为8. 其中sizeof(*p),实际上是int所占内存大小。
|