C++ Primer笔记(2.3.2)——指针
#include<iostream>
#include<cstdlib>
int main()
{
int *ip1, *ip2;
double dp, *dp2;
int val = 42;
int *pv = &val;
std::cout << "pv的值为:" << pv << "(即ival的地址)" << std::endl;
std::cout << std::endl;
double dval;
double *pd = &dval;
double *pd2 = pd;
int ival = 42;
int *p = &ival;
std::cout << "p指向的值为:" << *p << std::endl;
*p = 0;
std::cout << "p指向的值为:" << *p << std::endl;
std::cout << std::endl;
int *p1 = nullptr;
int *p2 = 0;
int *p3 = NULL;
int i = 42;
int *pi = 0;
int *pi2 = &i;
int *pi3;
std::cout << "pi 的值为:" << pi << std::endl;
std::cout << "pi2的值为:" << pi2 << " " << "pi2指向的值为:" << *pi2 << std::endl;
std::cout << std::endl;
pi3 = pi2;
pi2 = 0;
std::cout << "pi2的值为:" << pi2 << std::endl;
std::cout << "pi3的值为:" << pi3 << " " << "pi3指向的值为:" << *pi3 << std::endl;
std::cout << std::endl;
ival = 42;
pi = &ival;
std::cout << "pi的值为:" << pi << " " << "pi指向的值为:" << *pi << std::endl;
*pi = 0;
std::cout << "ival的值为:" << ival << std::endl;
std::cout << "pi的值为:" << pi << " " << "pi指向的值为:" << *pi << std::endl;
return 0;
}
``
|