指针的类型必须与由它保存其地址的变量的类型一致,当某个变量的地址给了指针p时,就可以通过*p(即表示该地址的数据)来对该变量数据进行操作
一定要牢记一个事实:指针所保存的是内存中的一个地址,它并不保存指向的数据的值本身。因此务必确保指针对应一个已经存在的变量或者一块已经分配的内存
*有两种用途,一是创建指针: int *p = &a; 另外是解引用 *p = 123;
c++允许多个指针指向同一个地址
.void *则为“无类型指针”,可以指向任何数据类型。对一个无类型指针进行解引用前必须先将他转换为一个适当的数据类型。
实例1:通过指针形式修改变量值(指针的解引用)
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a = 123; 7 float b = 3.14; 8 char c = 'c'; 9 unsigned long d = 19880808; 10 string e = "I love FishC!"; 11 12 cout << "a的值是:" << a << "\n"; 13 cout << "b的值是:" << b << "\n"; 14 cout << "c的值是:" << c << "\n"; 15 cout << "d的值是:" << d << "\n"; 16 cout << "e的值是:" << e << "\n\n"; 17 18 int *aPointer = &a; 19 float *bPointer = &b; 20 char *cPointer = &c; 21 unsigned long *dPointer = &d; 22 string *ePointer = &e; 23 24 *aPointer = 456; 25 *bPointer = 4.13; 26 *cPointer = 'F'; 27 *dPointer = 20190525; 28 *ePointer = "I love Beauty!"; 29 30 cout << "a的值是:" << a << "\n"; 31 cout << "b的值是:" << b << "\n"; 32 cout << "c的值是:" << c << "\n"; 33 cout << "d的值是:" << d << "\n"; 34 cout << "e的值是:" << e << "\n\n"; 35 36 }标签:02,变量,指向,int,数据类型,C++,地址,指针 From: https://www.cnblogs.com/ybqjymy/p/17640353.html