C++Primer 习题2.18-2.26
引用、指针、const
#include<iostream>
using namespace std;
int main()
{
int i = 2, j = 3;
int *p = &i;
cout << p << "," << *p << endl;
p = &j;
cout << p << "," << *p << endl;
*p = 5;
cout << p << "," << *p << endl;
j = 10;
cout << p << "," << *p << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i = 42;
int*p1 = &i;
*p1 = *p1**p1;
cout << *p1 << endl;
cout << 42 * 42;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i = 1;
int *p = &i;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a1 = 1, a0 = 0;
int *p1 = &a1;
int *p0 = &a0;
int *np = nullptr;
int *nnp;
if (a1)
cout << "a1 is true" << endl;
else
cout << "a1 is false" << endl;
if (a0)
cout << "a0 is true" << endl;
else
cout << "a0 is false" << endl;
if (p0)
cout << "p0 is true" << endl;
else
cout << "p0 is false" << endl;
if (*p0)
cout << "*p0 is true" << endl;
else
cout << "*p0 is false" << endl;
if (p1)
cout << "p1 is true" << endl;
else
cout << "p1 is false" << endl;
if (*p1)
cout << "*p1 is true" << endl;
else
cout << "*p1 is false" << endl;
if (np)
cout << "np is true" << endl;
else
cout << "np is false" << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i = 1;
void *p1 = &i;
cout <<"未赋值地址:"<< p1 << endl;
p1 = (void*)020;
cout <<"赋值后地址:"<< p1 << endl;
void *p2 = nullptr;
if (!p2)
cout << "void*类型能指定为空" << endl;
p2 = p1;
cout << "p1赋值给p2:"<<p2<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i = 0x10;
int &r = i;
cout <<"r的地址:"<< &r << endl;
cout <<"r的值:"<< r << endl;
int *p = &i;
int &r2 = *p;
cout <<"r2的值:"<< r2 << endl;
int **pp = &p;
cout << "*p地址:" << p << endl;
cout << "p的值:" << *p << endl;
cout << "**pp地址:" << pp << endl;
cout << "*pp地址:" << *pp << endl;
cout << "pp值:" << **pp << endl;
int *&r3 = p;
cout << "r3的地址:" << r3 << endl;
cout << "r3的值:" << *r3 << endl;
int **&r4 = pp;
cout << "r4的值:" << r4 << endl;
return 0;
}
/*输出示例
r的地址:007CF8A8
r的值:16
r2的值:16
*p地址:007CF8A8
p的值:16
**pp地址:007CF890
*pp地址:007CF8A8
pp值:16
r3的地址:007CF8A8
r3的值:16
r4的值:007CF890
*/
#include<iostream>
using namespace std;
int main()
{
const int c1 = 020;
const int &cr1 = c1;
cout <<"cr1的值:"<< cr1 << endl;
const int &cr2 = c1 * 2;
cout << "cr2被const常量赋值:" << cr2 << endl;
const int &cr3 = cr1 * 2;
cout << "cr3被const引用赋值:" << cr3 << endl;
const double i1 = 016.5;
double i2 = 016.5;
const int &cr4 = i1;
cout << "int的cr4被double16.5(const)赋值:" << cr4 << endl;
const double *cp1 = &i1;
double *p2 = &i2;
cout << "const*cp1被const地址i1赋值:" << *cp1 << endl;
cout << "此时的cp1地址:" << cp1 << endl;
cp1 = &i1;
cout << "给const*地址赋值合法但不会被改变:"<<cp1 << endl;
double i3 = 1008.6;
double *p3 = &i3;
cout << "i3地址:" << &i3 << endl;
double *const *const pc1 = &p3;
long double i4 = 3.1415;
cout << "i4地址:" << &i4 << endl;
cout << "p2地址:" << &p3 << endl;
**pc1 = i3;
cout << "**pc1的地址:" << &pc1 << endl;
cout << "*pc1的地址:" << *pc1 << endl;
cout << "pc1的地址:" << pc1 << endl;
cout << "pc1的值:" << **pc1 << endl;
return 0;
}