计算机程序设计基础精讲多练CC

上传人:平*** 文档编号:47641323 上传时间:2018-07-03 格式:PPT 页数:48 大小:939.86KB
返回 下载 相关 举报
计算机程序设计基础精讲多练CC_第1页
第1页 / 共48页
计算机程序设计基础精讲多练CC_第2页
第2页 / 共48页
计算机程序设计基础精讲多练CC_第3页
第3页 / 共48页
计算机程序设计基础精讲多练CC_第4页
第4页 / 共48页
计算机程序设计基础精讲多练CC_第5页
第5页 / 共48页
点击查看更多>>
资源描述

《计算机程序设计基础精讲多练CC》由会员分享,可在线阅读,更多相关《计算机程序设计基础精讲多练CC(48页珍藏版)》请在金锄头文库上搜索。

1、1第六章 指针和引用2教学目标l(1) 了解指针类型及引用类型的概念及二者的关系与区别;l(2) 了解指针与引用变量的使用与操作方法;l(3) 了解用指针访问变量、数组、字符串的方法。3基本内容 l6.1 指针l6.2 指针与数组l6.3 引用46.1 指针l6.1.1 地址l6.1.2 指针的定义l6.1.3 指针的初始化l6.1.4 指针的使用l6.1.5 函数的传址调用方式56.1.1 地址计算机的内存储器就象一个一维数组,每个数组元素就是一个存储单元。地址是存放信息数据的内存单元的编号。程序中定义的任何变量、数组或函数等,在编译时都会在内存中分配一个确定的地址单元。6如何表示地址?凡是

2、存放在内存中的程序和数据都有一个地 址,可以用它们占用的那片存储单元中的第一 个存储单元的地址表示。C+规定:l变量的地址: 可以用取地址运算符 int main() int x = 100; cout using namespace std; int main() int x = 7; int *ptr; ptr = cout using namespace std; void swap (int *xp, int *yp) int tmp; tmp = *xp; *xp = *yp; *yp = tmp; int main() int a = 2, b = 3;cout using nam

3、espace std; int main() int a=1, 2, 3, 4, 5; /int *ptr; /ptr = a; for(int i=0; i using namespace std; int mystrlen(char *string) char *ptr=string; while(*ptr!= 0) ptr +;/ 指针后移,直到指向字符串结束标志 return ptr-string; int main() char str = “How long am I?“; cout using namespace std; void swap (int x = y; y = tm

4、p; int main() int a = 1,b = 2; cout using namespace std; int main() int n; coutn; int *p =new intn+1; p0=0; p1=1; cout结结构体指针变针变 量名-成员变员变 量名 l例:Student stu1, stu2, *ptr = stu1.id = 20086035; ptr- id= 20091001;34结构体的初始化l可采用以下任意一种方式:l先声明结结构体变变量,再赋初值Student stu1; stu1.id = 20086035; strcpy(stu1.name, “Z

5、hang3“); stu1.score = 96;l声明时赋值:在声明结结构体变变量的同时对时对 其赋赋初值值Student stu2=20091001, “Li4“, 85.5;l注意:不能在定义结义结 构体类类型时给时给 成员变员变 量赋赋 值值35/ 例 6-9:学生信息-结构体 #include #include using namespace std; struct Student int id;/ 学号 char name20;/ 姓名 float score;/ 成绩 ; int main() Student stu1, stu2=20091001, “Li4“, 85.5; s

6、tu1.id=20086035; strcpy(stu1.name, “Zhang3“); stu1.score=96; cout using namespace std; void bubbleup(int *ptr, int count) for(int i=0; ii; j=j-1) if(*(ptr+j-1)*(ptr+j) int tmp = *(ptr+j-1); *(ptr+j-1) = *(ptr+j); *(ptr+j) = tmp; int main() int list = 53, 7, 12, 61, 90, 70, 87; int i; cout using name

7、space std; int main() char str=“Current:Clear, Wind: E at 6 km/h, Humidity: 30%“; char *ptr=str;/ 使指针ptr指向字符数组str int total, capital, small, numeral, others; / 定义各种计数器变量 total=capital=small=numeral=others=0;/ 初始化所有计数 器变量 while(*ptr!=0)/ 如果*ptr=0则表示字符串结束 total+;/ 总字符数 if(*ptr=A char *myltrim(char * s

8、tring) char *ptr=string; while(*ptr= ) ptr+; strcpy(string, ptr); return string; int main() char str = “ Who is Buddha?“; cout “截取前的原始字符串是: “ strendl; cout “截取空格后的字符串是: “ myltrim(str)endl; return 0; 运行结果: 截取前的原始字符串是: Who is Buddha? 截取空格后的字符串是: Who is Buddha?47学好程序设计语言的唯一途径是你的编程能力与你在计算机上投入的时间成结 束 语48

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

当前位置:首页 > 中学教育 > 教学课件

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