您的位置:首页 > 其它

第四章指针与数组

2014-06-19 22:48 267 查看
每一个程序在执行时都占用一个可用的内存空间,用于存放动态分配的对象,此内存空间称为程序的自由存储区或堆。

c语言使用malloc和free,c++使用new和delete来实现在自由存储区分配和回收内存;

string *psa=new string[10];//类类型,使用默认构造

int *pia=new int[10];//值类型,无初始化

int *pia2=new int[10]();//初始化为0

const int *pci_bad=new const int[100];//错误,无初始化同时不能修改值

cosnt int *pci_ok=new const int[100]();//初始化为0,不可修改

const string *pcs=new const string[100];//无意义,都是空串

char arr[0];//错误

char *cp=new char[0];//正确,但是cp不可以解引用,只可以:比较运算

指针数组:array of pointers,即用于存储指针的数组,也就是数组元素都是指针

数组指针:a pointer to an array,即指向数组的指针

int* a[4]     指针数组     

                 表示:数组a中的元素都为int型指针    

                 元素表示:*a[i]   *(a[i])是一样的,因为[]优先级高于*

int (*a)[4]   数组指针     

                 表示:指向数组a的指针

                 元素表示:(*a)[i]  

typedef int int_array[4];

int_array *ip=0;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: