4.19 addpntrs.cpp
#include <iostream>
int main()
{
using namespace std;
double wages[3] = { 10000.0, 20000.0, 30000.0 };
short stacks[3] = { 3, 2, 1 };
// Here are two ways to get the address of an array
double* pw = wages; // name of an array = address
short* ps = &stacks[0]; // or use address operator
// with array element
cout << "pw = " << pw << ", *pw = " << *pw << endl;
pw = pw + 1;
cout << "add 1 to the pw pointer:\n";
cout << "pw = " << pw << ", *pw = " << *pw << "\n\n";
cout << "ps = " << ps << ", *ps = " << *ps << endl;
ps = ps + 1;
cout << "add 1 to the ps pointer:\n";
cout << "ps = " << ps << ", *ps = " << *ps << "\n\n";
cout << "access two elements with array notation\n";
cout << "stacks[0] = " << stacks[0]
<< ", stacks[1] = " << stacks[1] << endl;
cout << "access two elements with pointer notation\n";
cout << "*stacks = " << *stacks
<< ", *(stacks + 1) = " << *(stacks + 1) << endl;
cout << sizeof(wages) << " = size of wages array\n";
cout << sizeof(pw) << " = size of pw pointer\n";
// cin.get();
return 0;
}
-
该代码主要探究数组名也是第一个元素的地址
-
pw与ps两者实现的方式不同,但结果相同,pw+1其地址值加8(元素类型的大小),以便指针指向下一个元素
4.20 ptrstr.cpp
#include <iostream>
#include <cstring> // declare strlen(), strcpy()
int main()
{
using namespace std;
char animal[20] = "bear"; // animal holds bear
const char* bird = "wren"; // bird holds address of string
char* ps; // uninitialized
cout << animal << " and "; // display bear
cout << bird << "\n"; // display wren
// cout << ps << "\n"; //may display garbage, may cause a crash
cout << "Enter a kind of animal: ";
cin >> animal; // ok if input < 20 chars
// cin >> ps; Too horrible a blunder to try; ps doesn't
// point to allocated space
ps = animal; // set ps to point to string
cout << ps << "!\n"; // ok, same as using animal
cout << "Before using strcpy():\n";
cout << animal << " at " << (int*)animal << endl;
cout << ps << " at " << (int*)ps << endl;
ps = new char[strlen(animal) + 1]; // get new storage
strcpy(ps, animal); // copy string to new storage
cout << "After using strcpy():\n";
cout << animal << " at " << (int*)animal << endl;
cout << ps << " at " << (int*)ps << endl;
delete[] ps;
// cin.get();
// cin.get();
return 0;
}
- 本函数探究了字符串和指针的联合应用
- 首先const char * bird = "wren";"wren"实际是字符串的地址,该代码是将“wren”的地址赋给bird指针
- 而后则是应用了之前的new创建动态数组
4.21 newstrct.cpp
#include <iostream>
struct inflatable // structure definition
{
char name[20];
float volume;
double price;
};
int main()
{
using namespace std;
inflatable* ps = new inflatable; // allot memory for structure
cout << "Enter name of inflatable item: ";
cin.get(ps->name, 20); // method 1 for member access
cout << "Enter volume in cubic feet: ";
cin >> (*ps).volume; // method 2 for member access
cout << "Enter price: $";
cin >> ps->price;
cout << "Name: " << (*ps).name << endl; // method 2
cout << "Volume: " << ps->volume << " cubic feet\n"; // method 1
cout << "Price: $" << ps->price << endl; // method 1
delete ps; // free memory used by structure
// cin.get();
// cin.get();
return 0;
}
- 该代码介绍了动态结构的创建和应用,其创建方法与动态数组类似
- 同样,“动态”意味着内存是在运行时,而非创建时分配的
- 之后则演示了两种方位结构成员的指针表示法:ps->name 以及(*ps).volme,即在结构体名的情况下使用.,指针的情况下使用->
4.22 delete.cpp
#include <iostream>
#include <cstring> // or string.h
using namespace std;
char* getname(void); // function prototype
int main()
{
char* name; // create pointer but no storage
name = getname(); // assign address of string to name
cout << name << " at " << (int*)name << "\n";
delete[] name; // memory freed
name = getname(); // reuse freed memory
cout << name << " at " << (int*)name << "\n";
delete[] name; // memory freed again
// cin.get();
// cin.get();
return 0;
}
char* getname() // return pointer to new string
{
char temp[80]; // temporary storage
cout << "Enter last name: ";
cin >> temp;
char* pn = new char[strlen(temp) + 1];
strcpy(pn, temp); // copy string into smaller space
return pn; // temp lost when function ends
}
- 该代码介绍了使用new和delete来储存通过键盘输入的字符串实例