学习《C++ Primer Plus》06 -2

指针程序示例:

1.简单运算

//pointer.cpp

#include<iostream>

int main()

{

    using namespace std;

    int updates = 6;

    int *p_updates;

    p_updates = &updates;

    //value

    cout << "Value:updates = " << updates;

    cout << ",*p_updates = " << *p_updates << endl;

    //address

    cout << "Address: &updates= " << &updates;

    cout << ",p_update= " << p_updates;

    cout << ",&p_updates= " << &p_updates;

    //use pointer to change value:

    *p_updates += 1;

    cout << "Now updates= " << updates<<endl;

    cout << *p_updates;

    cin.get();

    return 0;

   

}

注意:在*p_updates+=1时,本来想用*p_updates++替换,结果只是地址加1,该指针指向的地址改变了,原因出在运算符的优先运算级别。

应改为(*p_updates)++!!!

 

2.指针和new初级

//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 here

 

    cout << "nights value = "

        << nights << " : location = " << &nights << endl;

    cout << "new int value = "

        << *pt << " : location = " << pt << endl;

 

    double *pd = new double;

    *pd = 1001.0;

 

    cout << "double value = "

        << *pd << " : location = " << pd << endl;

    cout << "location of pointer pd:" << &pd << endl;

    cout <<"sizeof(pt)"<< sizeof(pt) << endl;

    cout << "sizeof(*pt): " << sizeof(*pt) << endl;

    cout << "sizeof(pt)" << sizeof(pd) << endl;

    cout << "sizeof(*pt): " << sizeof(*pd) << endl;

    cin.get();

    return 0;

}

注意:该程序运行结果指出,new分配的内存块通常与常规变量分配的内存块不同。变量nights和pd的值都存储在被称为栈(stack)的内存区域内,而new从被称为堆(heap)或自由存储区的内存区域分配内存。

 

3.new和数组

//arraynew.cpp

#include<iostream>

int main()

{

    using namespace std;

    double *p3 = new double[3];     //space for 3 doubles

    p3[0] = 0.1;                     //treat p3 like an array name

    p3[1] = 0.2;

    p3[2] = 0.3;

    cout << "p3[1] = " << p3[1] << endl;

    p3 += 1;                         //increment the pointer

    cout << "Now p3[0] = " << p3[0] << endl;

    cout << " and the p3[1] = " << p3[1] << endl;

    p3 = p3 - 1;                     //pointer back to the beginning

    delete[]p3;                      //free the memory

    cin.get();

    return 0;

}

注意:指针最后必须指向原来的值,这样程序可以给delete[]提供正确的地址。

 

3.指针和字符串

//ptrstr.cpp--uaing pointers to strings

#include<iostream>

#include<cstring>

int main()

{

    using namespace std;

    char animal[20] = "bear";

    const char *bird = "wren";      //bird holds address of string

    char *ps;

    cout << animal << " and " << bird << endl;

    cout << "Enter a kind of animal:";

    cin >> animal;

     

    ps = animal;

    cout << ps << endl;

    cout << "Before using strcpy():\n";

    cout << animal << " at " << (int*)animal << endl;

    cout << ps << " at " << (int*)ps << endl;

}

警告:在将字符串读入程序时,应使用已经分配的内存地址。该地址可以是数组名,也可以是使用new初始化的指针。

一般来说,如果给cout提供一个指针,它将打印地址。但如果指针的类型为char*,则cout将显示指向的字符串。如果要显示的是字符串的地址,则必须将这种指针强制转换为另一种指针类型,如int*。

 

4.使用new创建动态结构

//newstrct.cpp--using new with a struct

#include<iostream>

struct inflatable

{

    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);

    cout << "Enter volume in cubic feet: ";

    cin >> (*ps).volume;

    cout << "Enter price: $";

    cin >> ps->price;

    cout << "Name: " << (*ps).name << endl;

    cout << "Volume: " << ps->volume << endl;

    cout << "Price: $: " << ps->price << endl;

    delete ps;

    cin.get();

    cin.get();

    return 0;

}

    如果ps是指向结构的指针,则*ps就是被指向的值—结构本身。

 

5.一个使用new和delete的示例

//delete.cpp--using the delete operator

#pragma   warning(disable:4996)

#include<iostream>

#include<string.h>

using namespace std;

char* getname(void);  //function prototype

 

int main()

{

    char* name;

    name = getname();

    cout << name << " at " << (int*)name << endl;

    delete[]name;

 

    name = getname();  //reuse freed memory

    cout << name << " at " << (int*)name << endl;

    delete[]name;

 

    cin.get();   //只要有cin 多加一个cin.get

    cin.get();

    return 0;

}

 

char* getname()

{

    char temp[80];

    cout << "Enter last name: ";

    cin>>temp;

    char * pn = new char[strlen(temp) + 1];

    strcpy(pn, temp);

    return pn;

}

关于用[strlen(temp) + 1]

C-风格字符串具有一种特殊的性质:以空字符结尾(null character),空字符被写作\0。

 

6.类型组合

已经学习了数组、结构和指针,可以用各种方式组合它们。

//mixtypes.cpp--some type combinations

#include<iostream>

 

struct years

{

    int year;

};

 

int main()

{

    using std::cout;

    years s0, s1, s2;

    s0.year = 1998;

    years *pa = &s1;

    pa->year = 1999;

    years tri[3];

    tri[0].year = 2003;

    cout << tri->year<<'\n';

    const years *arp[3] = { &s0,&s1,&s2 };

    cout << arp[1]->year << '\n';

    const years **ppa = arp;

    auto ppb = arp;     //C++11 automatic type deduction

    //or use const years **ppb=arp

    cout << (*ppa)->year << '\n';

    cout << (*(ppb + 1))->year << std::endl;

 

    std::cin.get();

    return 0;

}

 

7.比较数组、vector对象和array对象

//choice.cpp--array variations

#include<iostream>

#include<vector>

#include<array>

 

int main()

{

    using namespace std;

    //C, original C++

    double a1[4] = { 1.2,2.4,3.6,4.8 };

    //C++98 STL

    vector<double>a2(4);  //creat vector with 4 elements

    //no simple way to initialize in C98

    a2[0] = 1.0 / 3.0;

    a2[1] = 1.0 / 5.0;

    a2[2] = 1.0 / 7.0;

    a2[3] = 1.0 / 9.0;

    //C++11--create and initialize array object

    array<double, 4>a3 = { 3.14,2.72 ,1.62,1.41 };

    array<double, 4>a4;

    a4 = a3;             //valid for array object of same size

    //use array notation

    cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;

    cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;

    cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;

    cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;

    //misdeed

    a1[-2] = 20.2;

    cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;

    cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;

    cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;

 

    cin.get();

    return 0;

}

三种都可以用标准数组表示法来访问各个元素。从返回地址可知,array对象和数组存储在相同的内存区域(即栈)中,而vector对象存储在另一个区域(自由存储区域或堆)中,第三,注意到可以将一个array对象赋给另一个array对象;而对于数组,必须逐个元素复制数据。

    a1[-2] = 20.2;

索引-2表示:

*(a1-2) = 20.2 ;

找到a1所指定的地方,向前移动两个double元素,并将20.2存储到目的地址中。

 

 

猜你喜欢

转载自blog.csdn.net/ly_222222/article/details/81168169