//
// main.cpp
// point1
//
// Created by 炒饭 on 2019/4/14.
// Copyright © 2019 炒饭. All rights reserved.
//
//指针和变量值硬币的两面性
#include <iostream>
int main()
{
// insert code here...
using namespace std;
int updates = 6;
int * p_updates;
p_updates = &updates;
//指针的地址初始化
int higgens = 5;
int *pta= &higgens;
int nights =1001;
int *pt = new int;
*pt = 1001;
cout << "nights value =";
cout << nights << ": location " << &nights <<endl;
cout <<"int ";
cout << "value = "<< *pt << ": location = "<< pt << endl;
delete pt;
//new创建和释放指针数组
//type_name *pointer_name = new type_name [num_elements];pointer_name指向第一个元素
short * ps = new short [500];
delete [] ps;
//访问动态数组的方法,将指针变量作为数组名
double * p3 = new double [3];
p3[0]=0.2;
p3[1]=0.5;
p3[2]=0.8;
cout << "p3[1] is "<< p3[1] << ".\n";
p3 = p3+1;
cout <<" Now p3[0] is "<< p3[0] <<" and " ;
cout << "p3[1] is "<< p3[1] << ".\n";
p3= p3-1;
delete [] p3;
//表示值1
cout << "Value: updates = " << updates;
cout << ", *p_updates = "<< *p_updates <<endl;
//表示地址
cout << "Adresses: &updates =" << &updates;
cout << ", p_updates= "<< p_updates <<endl;
//改变值
*p_updates = *p_updates +1;
cout << "Now updates = "<< updates << endl;
std::cout << "Hello, World!\n";
return 0;
}
实验结果:
nights value =1001: location 0x7ffeefbff51c
int value = 1001: location = 0x10280ca30
p3[1] is 0.5.
Now p3[0] is 0.5 and p3[1] is 0.8.
Value: updates = 6, *p_updates = 6
Adresses: &updates =0x7ffeefbff538, p_updates= 0x7ffeefbff538
Now updates = 7
Hello, World!
Program ended with exit code: 0