文档详情

第 4 章(2)━━指针和数组.ppt

发布:2017-07-29约8.1千字共39页下载文档
文本预览下载声明
C++程序设计;主要内容;指针的概念;【例】 #includeiostream.h void main ( ) { int a = 3 ; double b = 5.6 ; char c = ‘A’ ; cout “a的地址=” a “\ta的内容=” a ; cout “\t字节数=” sizeof( a ) endl ; cout “a=” *a endl ; cout “b的地址=” b “\tb的内容=” b ; cout “\t字节数=” sizeof( b ) endl ; cout “b=” *b endl ; cout “c的地址=” c “\tc的内容=” c ; cout “\t字节数=” sizeof( c ) endl ; cout “c=” *c endl ; };指针变量的定义和使用;指针变量的定义和使用;指针变量的定义和使用;【例】 #includeiostream.h void main ( ) { int a1 = 3 , a2 = 7 , *pa = a1 ; double b1 = 5.6 , *pb = b1 ; float b2 = 3.7 ; cout “a1:\t地址=” pa “\t内容=” *pa endl ; cout “pa:\t地址=” pa “\t内容=” pa endl ; pa = a2 ; cout “a2:\t地址=” pa “\t内容=” *pa endl ; cout “pa:\t地址=” pa “\t内容=” pa endl ; cout “b1:\t地址=” pb “\t内容=” *pb endl ; cout “pb:\t地址=” pb “\t内容=” pb endl ; pb = ( double * ) b2 ; //写成 pb = b2 为何编译通不过? cout “b2:\t地址=” pb “\t内容=” *pb endl ; cout “pb:\t地址=” pb “\t内容=” pb endl ; };指针的赋值运算;指针的赋值运算;指针的算术运算;指针的算术运算;指针的关系运算;【例】 #includeiostream.h void main ( ) { int x[ 8 ] = { 2 , 8 , 3 , 7 , 9 , 4 , 6 , 1 } ; int a=10 , b=20 , *p1 = b , *p2 = x[ 0 ] ; char c[ 8 ] = “ABCDEF” , *p3 = c ; cout “*p1=” *p1 “\tp1=” (int) p1 endl ; p1++ ; cout “*p1=” *p1 “\tp1=” (int) p1 endl ; cout “*p2=” *p2 “\tp2=” (int) p2 endl ; p2 += 4 ; cout “*p2=” *p2 “\tp2=” (int) p2 endl ; p2 -= 2 ; cout “*p2=” *p2 “\tp2=” (int) p2 endl ; cout “*p3=” *p3 “\tp3=” (int) p3 endl ; p3++ ; cout “*p3=” *p3 “\tp3=” (int) p3 endl ; cout “*p3=” *p3 “\tp3=” p3 endl ; cout “*p3=” *p3 “\tp3+2=” p3+2 endl ; } ;指针的混合运算;【例】 #includeiostream.h void main ( ) { int x[ 8 ] = { 2 , 8 , 3 , 7 , 9 , 4 , 6 , 1 } ; int *p = x[ 0 ] ; cout “x[0]=” x[ 0 ] endl ; cout “p=” (int) p endl ; cout “*p=” *p endl ; cout “表达式 (*p)++ 的值=” (*p)++ endl ; cout “x[0]=” x[ 0 ]
显示全部
相似文档