1.类型和指针的关系
指针的类型决定了指针的解析能力,如char类型,解析1字节 int类型 解析4字节
int main()
{
int a = 0x12345678;
int* ip = &a;
short* sp = (short*)&a;
char* cp = (char*)&a;
int ix = *ip;//12345678
short sx = *sp;//5678
char cx = *cp;//78
return 0;
}
int main()
{
int ar[5] = { 1,2,3,4,5 };
int* p = &ar[0];
p = p + 3;
//p=p%3;
return 0;
}
指针只能进行加减运算,不能进行乘和取模。
int main()
{
int a = 0x12345678;
int* ip = &a;
//char* cp = &a;类型不兼容
//char* p = &a;
return 0;
}
基本数据类型可以相互赋值,指针变量的类型不同就不能相互赋值
2.取值和取地址运算符的结合
vint main()
{
int a = 10, b = 20;
int* p = &a;
a = 100;
*&a = 100;
//&*a;右操作数必须是指针
*&a;
*& b;
&*p;
}
*&a => a; *&p =>p; &*p => &a