4.14 address.cpp
#include <iostream>
int main()
{
using namespace std;
int donuts = 6;
double cups = 4.5;
cout << "donuts value = " << donuts;
cout << " and donuts address = " << &donuts << endl;
// NOTE: you may need to use unsigned (&donuts)
// and unsigned (&cups)
cout << "cups value = " << cups;
cout << " and cups address = " << &cups << endl;
// cin.get();
return 0;
}
- 该代码指出了指针这一特性,与c语言相同
![](https://img-blog.csdnimg.cn/direct/9b79328d944542d2875d8da79fb700e5.png)
4.15 pointer.cpp、
#include <iostream>
int main()
{
using namespace std;
int updates = 6; // declare a variable
int * p_updates; // declare pointer to an int
p_updates = &updates; // assign address of int to pointer
// express values two ways
cout << "Values: updates = " << updates;
cout << ", *p_updates = " << *p_updates << endl;
// express address two ways
cout << "Addresses: &updates = " << &updates;
cout << ", p_updates = " << p_updates << endl;
// use pointer to change value
*p_updates = *p_updates + 1;
cout << "Now updates = " << updates << endl;
// cin.get();
return 0;
}
- 同样,该代码探究了*运算符和解引用&的用法,与c语言相同
![](https://img-blog.csdnimg.cn/direct/39b04886ff5a47c194d95ccbc5784863.png)
4.16 init_ptr.cpp
#include <iostream>
int main()
{
using namespace std;
int higgens = 5;
int * pt = &higgens;
cout << "Value of higgens = " << higgens
<< "; Address of higgens = " << &higgens << endl;
cout << "Value of *pt = " << *pt
<< "; Value of pt = " << pt << endl;
// cin.get();
return 0;
}
- 与上述代码相同,讲述了指针的应用,int*是一种复合类型,是指向int的指针
![](https://img-blog.csdnimg.cn/direct/6c15b7e521134bf49c673688952e3c9d.png)
4.17 use_new.cpp
#include <iostream>
int main()
{
using namespace std;
int nights = 1001;
int * pt = new int; // allocate space for an int
*pt = 1001; // store a value there
cout << "nights value = ";
cout << nights << ": location " << &nights << endl;
cout << "int ";
cout << "value = " << *pt << ": location = " << pt << endl;
double * pd = new double; // allocate space for a double
*pd = 10000001.0; // store a double there
cout << "double ";
cout << "value = " << *pd << ": location = " << pd << endl;
cout << "location of pointer pd: " << &pd << endl;
cout << "size of pt = " << sizeof(pt);
cout << ": size of *pt = " << sizeof(*pt) << endl;
cout << "size of pd = " << sizeof pd;
cout << ": size of *pd = " << sizeof(*pd) << endl;
// cin.get();
return 0;
}
- 该代码使用new分别为int和double类型的数据对象分配内存
- 这两个都是在这两者赋值前进行内存分配的
- 在分配后,就可以像使用变量一样来使用*pt和*pd
- 其中new分配的内存在堆区或自由储存区
![](https://img-blog.csdnimg.cn/direct/36fd66aaca55434db475b48c6968a90d.png)
4.18 arraynew.cpp
#include <iostream>
int main()
{
using namespace std;
double * p3 = new double [3]; // space for 3 doubles
p3[0] = 0.2; // treat p3 like an array name
p3[1] = 0.5;
p3[2] = 0.8;
cout << "p3[1] is " << p3[1] << ".\n";
p3 = p3 + 1; // increment the pointer
cout << "Now p3[0] is " << p3[0] << " and ";
cout << "p3[1] is " << p3[1] << ".\n";
p3 = p3 - 1; // point back to beginning
delete [] p3; // free the memory
// cin.get();
return 0;
}
- 该代码介绍了使用new的动态数组的创建以及应用
- 动态数组可以在运行过程中选择数组的长度
- 首先是创建的方法:double* p3 = new double 【3】,与之前分配内存相似
- 之后则是c语言相同的套路,指针即是数组名,+1则指向下一个元素
- 最后切记要使用delete【】p3来释放之前的内存