指针(pointer)——c_c++的精髓

上传人:F****n 文档编号:103909271 上传时间:2019-10-08 格式:DOCX 页数:8 大小:22.04KB
返回 下载 相关 举报
指针(pointer)——c_c++的精髓_第1页
第1页 / 共8页
指针(pointer)——c_c++的精髓_第2页
第2页 / 共8页
指针(pointer)——c_c++的精髓_第3页
第3页 / 共8页
指针(pointer)——c_c++的精髓_第4页
第4页 / 共8页
指针(pointer)——c_c++的精髓_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《指针(pointer)——c_c++的精髓》由会员分享,可在线阅读,更多相关《指针(pointer)——c_c++的精髓(8页珍藏版)》请在金锄头文库上搜索。

1、指针(pointer)C/C+的精髓!你不得不学好它! 指针(pointer)指针是一种数据类型,用于存放某个变量的地址值。此时该指针被称为是指向该变量。一个指针的类型决定于它所指向的变量的类型。指针既可指向基本数据类型(即预定义数据类型),又可指向数组、函数、类和文件等用户自定义数据类型。指针可以初始化为0、NULL(即0,这是标准库头文件中定义的符号化常量)或一个地址。内容为0或NULL的指针不指向任何变量,称为空指针。例如:int a;int *ptr;指针ptr在下式中被初始化为指向变量a。ptr = &a;两条语句也可组合地表示为:a = *ptr;其中&为存地址运算符,而*则为取内

2、容运算符。ptr a int a, *ptr;ptr = &a;指针与数组数组名是一个指向该数组的常量指针(其值不能改变),也是该数组中首单元的地址值。一维数组的指针如下:int arr5 = 1, 3, 5, 7, 9 ;int *ptra;ptra = arr;ptra arr0=1 arr1=3 arr2=5 arr3=7 arr4=9 数组指针与数组名两者的异同:(1)整型变量指针ptr与整型数组指针ptra的说明格式相同,其间空格可放可不放。(2)arr既是数组名,又是数组地址,还是数组指针(称为常量指针),三位一体。因此arr可在一定范围内与ptra等效地使用。 可在一定范围内与

3、* 等效地使用。但arr不准重新赋值。例如:arr0即*arr即*ptra即ptra0 = 1arr3即*(arr+3)即*(ptra+3)即ptra3 = 7但arr的使用不如ptra灵活,如:不允许*arr+,而允许*ptra+。见以下例子:2.2中例1的主程序void main() int arr5 = 1, 3, 5, 7, 9; for ( int i = 0; i 5; i+) cout arri endl; /数组名其中的for语句可改为: for ( int i = 0; i 5; i+) cout *(arr+i) endl; /指针 / 但不允许arr加减,即不允许: /

4、cout *(arr+) endl; not allowed但可使用以下指针ptra: int *ptra = arr; for ( int i = 0; i 5; i+) cout *(ptra+) endl; / 此处可以将指针加减(3)字符串与字符数组:字符串一般使用指针(包括变量指针和常量指针两种方式)表示其地址和内容,此地址也即其第一个字符的地址。第一种变量指针方式例如:char *ch_ptr = home; (ch_ptr为变量指针)。第二种常量指针方式例如:int arr5 = 1, 3, 5, 7, 9; (arr为常量指针)。此处字符数组名是一个指向该数组的常量指针,它不能

5、重新赋值。例两种指针方式及其地址和字符串内容/ str_ptr_&_array_1.cpp#include #include void main() char *nm = Nice; char name = OK; coutAddresses of the pointers nm and name themselves:endl; coutnm is at &nm, name is at &nameendl; couthexsetiosflags(ios:uppercase)nm points to 0x(int)nm; cout, name points to 0x(int)nameendl

6、; coutCharacter strings are nm and ; coutname respectively.endl;/* Results:Addresses of the pointers nm and name themselves:nm is at 0x0065FDF4(栈区), name is at 0x0065FDF0(栈区)nm points to 0x42608C(数据区), name points to 0x65FDF0(栈区)Character strings are Nice and OK respectively.*/说明:我在编程时的数据与此相差很大,读者需要

7、自行地将上述代码运行一下.程序中cout(int)name用于读取指针内容即指针所指向的地址(字符串地址);而coutname则用于读取该指针所指向的字符串内容。指向指针的指针例如:int i=5;int *ptr = &i;int *pp = &ptr;即:指向指针ptr的指针 整型变量i的指针 整型变量pp ptr = &i i = 5 可表示为:i = *ptr;或i = *pp;指针数组(array of pointers)在实际应用中,常常使用一维一级字符指针数组来存放一些字符串,以便于操作。例使用一维一级字符指针数组来存放一星期中的各天。/ arr_ptr1.cpp/ Show t

8、he function of a character array#include char *name = ,Monday,Tuesday,Wednesday,Thursday, Friday, Saturday, Sunday; / 第一单元空着,因数组从零开始,/ 而生活中又没有星期零void main() int week; while (1) coutweek; if ( week7 ) break; coutThis is nameweekendl; coutProgram ended!endl;/* Results:Input sequential number(1-7):2Thi

9、s is TuesdayInput sequential number(1-7):4This is ThursdayInput sequential number(1-7):0Program ended! */深入贯彻落实科学发展观和党的十七届五中全会,及全国、省、市纪委工作会议精神,坚持以人为本、执政为民理念,坚持标本兼治、综合治理、惩防并举、注重预防的方针,弘扬理论联系实际的马克思主义学风several group number, then with b a, =c,c is is methyl b two vertical box between of accurate size. Pe

10、r-23 measurement, such as proceeds of c values are equal and equal to the design value, then the vertical installation accurate. For example a, b, and c valueswhile on horizontal vertical errors for measurement, General in iron angle code bit at measurement level points grid errors, specific method is from baseline to methyl vertical box center line distance for a, to b vertical box distance for b, list can measured

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号