浙江大学C颜晖原版C8_第1页
浙江大学C颜晖原版C8_第2页
浙江大学C颜晖原版C8_第3页
浙江大学C颜晖原版C8_第4页
浙江大学C颜晖原版C8_第5页
已阅读5页,还剩84页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第八章指针 指针指针和数组指针和字符串指针数组和二级指针指针和函数 8 1指针 intx x 3 printf d x 直接访问 通过变量名直接访问地址 间接访问 把变量的地址放到另一变量中 使用时先找到后者的地址 从中取出前者的地址 指针变量 存放地址的变量 某个变量的地址 指向 指向 intx x 3 printf d x 8 1 1指针变量的定义 指针变量所指向变量的类型 int px px是整型指针 指向整型变量float pf pf是浮点型指针 指向浮点型变量char pc pc是字符型指针 指向字符型变量 类型名 指针变量名 类型名 指针变量名 int p1 p2 等价于int p1 int p2 int px 注意 指针变量名是px 不是 px 8 1 2指针的基本操作 p p所指向的变量a p 1 把a的地址赋给p 即p指向a 输入57 输出 3 35 57 710 10 main inta 3 p p 例8 1指针运算 输出 100 10100 10 main inta b int p1 p2 a 100 b 10 p1 例8 2指针运算 指针运算注意事项 1 当p a后 p与a相同 2 int p 定义 p 10 p所指向的变量 3 p与 a相同 是地址 a与a相同 是变量 当p a时 4 p 等价于a 将p所指向的变量值加1 p 等价于 p 先取 p 然后p自加 此时p不再指向a 2 赋值 p2 p1 p2也指向a p1 p2 int p1 p2 a 3 p1 把a的地址赋给p1 即p1指向a 输出 10 100 例8 3指针赋值 main inta b int p1 p2 a 100 b 10 p1 8 1 3指针变量的初始化 例8 4 1voidmain inta 1 b 2 int p1 8 1 3指针变量的初始化 例8 4 2voidmain inta 1 b 2 int p1 8 1 4指针作为函数的参数 swap1 intx inty intt t x x y y t 输出 3 5 例8 5main inta 3 b 5 swap1 a b printf d d n a b swap2 int p1 int p2 intt t p1 p1 p2 p2 t 输出 5 3 值传递 地址未变 但存放的变量的值改变了 main inta 3 b 5 swap2 swap3 int p1 int p2 int p p p1 p1 p2 p2 p 值传递 形参指针的改变不会影响实参 main inta 3 b 5 swap2 输出 3 5 swap2 int p1 int p2 intt t p1 p1 p2 p2 t main inta 3 b 5 swap2 要使某个变量的值通过函数调用发生改变 1 在主调函数中 把该变量的地址作为实参 2 在被调函数中 用形参 指针 接受地址 3 在被调函数中 改变形参 指针 所指向变量的值 例8 6 1指针作为函数的参数 voidp int a intb a a 1 b voidmain intx 3 y 5 p 调用前main x 3 y 5调用p ab3526调用后main x 2 y 5 将变量的地址作为实参 调用后 该变量的值可能改变 例8 6 2 voidp int a intb a a 1 b voidmain intx 3 y 4 p 调用前main x 3 y 4调用p ab4334调用后main x 3 y 3 例8 6 3 voidp int a intb a a 1 b voidmain intx 3 y 5 p 调用前main x 3 y 5第1次调用p ab3526调用后main x 2 y 5第2次调用p ab5243调用后main x 2 y 4 例7 9 自定义函数dayofyear year month day 返回该年的第几天 dayofyear 2000 3 1 返回61dayofyear 1981 3 1 返回60分析 月0123 1112非闰年03128313031闰年03129313031 inttab 2 13 0 31 28 31 30 31 30 31 31 30 31 30 31 0 31 29 31 30 31 30 31 31 30 31 30 31 例7 9 voiddayofyear intyear intmonth intday intk leap inttab 2 13 0 31 28 31 30 31 30 31 31 30 31 30 31 0 31 29 31 30 31 30 31 31 30 31 30 31 leap year 4 0 例8 7 输入年和天数 输出对应的月和日 例如 输入2000和61 输出3和1 自定义函数monthday year yearday pmonth pday 带回2个结果 用2个指针作为函数的参数 voidmain intday month year yearday voidmonth day intyear intyearday int pmonth int pday printf inputyearandyearday n scanf d d 例8 7 voidmonth day intyear intyearday int pmonth int pday intk leap inttab 2 13 0 31 28 31 30 31 30 31 31 30 31 30 31 0 31 29 31 30 31 30 31 31 30 31 30 31 leap year 4 0 8 2指针和数组 8 2 1指针 数组 地址间的关系8 2 2数组名做为函数的参数 8 2 1指针 数组 地址间的关系 指针和数组有密切的关系任何由数组下标来实现的操作都能用指针来完成 inta 10 数组名是一个指针它的值是数组首元素的地址即它指向数组的首元素 int ap ap ap指向数组a的首元素 a i a i 或 ap a ap ap i ap i a i 的地址 a i a i ap ia i 相当于 a i ap i ap i ap for i 0 i 10 i printf d a i for ap a ap a 10 ap printf d ap 注意 数组名a是指针常量 不能a for i 0 i 10 i printf d a i 例 输出数组a所有元素 1 数组元素作为函数实参函数形参为变量 与变量作为函数实参相同 值传递 2 数组名作为函数参数由于数组名是指针常量 相当于指针作为函数的参数数组名做为实参形参是指针变量 数组 8 2 2数组名作为函数的参数 floataverage float array inti floataver sum 0 for i 0 i 10 i sum array i aver sum 10 return aver main floatscore 10 aver inti for i 0 i 10 i scanf f 1 实参是数组名 2 形参是指针变量可以写成数组形式可以不指定长度 例8 8求平均分 floataverage float array inti floataver sum 0 for i 0 i 10 i sum array i aver sum 10 return aver main floatscore 10 aver inti for i 0 i 10 i scanf f array i 3 若在函数中只处理部分数组元素 用参数指定个数 floataverage float array intn inti floataver sum 0 for i 0 i n i sum array i aver sum n return aver main floatscore 20 aver inti for i 0 i 20 i scanf f 4 数组名做为函数的参数 在函数调用时 将实参数组首元素的地址传给形参 指针变量 因此 形参也指向实参数组的首元素 如果改变形参所指向单元的值 就是改变实参数组首元素的值 或 形参数组和实参数组共用同一段存贮空间 如果形参数组中元素的值发生变化 实参数组中元素的值也同时发生变化 例8 9冒泡法排序 98888854408955554504559444605444966066666908000009 相邻两个数比较 小的调到前面 a j a j 1 j 0to4 j 0to3 j 0to2 j 0to6 1 i 98888854408955554504559444605444966066666908000009 985460i 1j 0 895460j 1 859460j 2 854960j 3 854690j 4 854609 main inti j n t a 10 n 6 for i 0 ia j 1 t a j a j a j 1 a j 1 t main inti a 10 for i 0 i 10 i scanf d voidsort int array intn inti j t for i 1 iarray j 1 t array j array j array j 1 array j 1 t 8 3指针和字符串 8 3 1常用的字符串处理函数8 3 2字符串的指针表示8 3 3字符数组和字符指针 8 3 1常用的字符串处理函数 1 使用printf函数和scanf函数输入输出字符串用 s格式 将字符串一次性输入输出staticcharstr 20 Hello 输出printf s Hello printf s str 注意 遇到 0 输出结束for i 0 str i 0 i putchar str i 字符数组名 输出 HelloHello 输入scanf s str 注意 输入参数使用数组名 不加地址符 遇到回车或空格 输入结束 并自动在末尾加 0 输入Howareyou str中 while str i getchar i s i 0 输入多个用空格分隔的字符串chars1 20 s2 20 s3 20 scanf s s s s1 s2 s3 s1 s2 s3 字符串可以一次性输入输出一般的字符数组只能逐个字符输入输出 输入Howareyou 2 字符串输出函数puts staticcharstr 20 hello puts str 输出 Hello puts World 输出 World 输出 HelloWorld puts str 与printf s str 的区别 puts str printf s n str 自动将结束符 0 转换成 n puts Hello puts World 输出 HelloWorld printf s Hello printf s World staticcharstr 20 gets str 字符数组名 3 字符串输入函数gets 输入遇空格不结束 只有遇回车才结束 gets str 输入 hello str中 hello 0 gets str 与scanf s str 的区别 遇到回车或空格 输入结束 例 gets str 输入 Howareyou Str Howareyou 0scanf s str 输入 Howareyou Str How 0 遇回车 输入结束 strcpy str1 str2 将字符串str2复制到str1中staticcharstr1 20 staticcharstr2 20 happy 4 字符串复制函数strcpy strcpy str1 str2 str1中 strcpy str1 world str1中 world 0 include stdio h include string h main charstr1 20 str2 20 gets str2 strcpy str1 str2 puts str1 输入 1234 输出 1234 strcat str1 str2 连接两个字符串str1和str2 并将结果放入str1中 5 字符串连接函数strcat include stdio h include string h main charstr1 80 str2 20 gets str1 gets str2 strcat str1 str2 puts str1 输入 Letusgo 输出 Letusgo str1中 Letus 0str2中 go 0 str1中 Letusgo 0 strcmp str1 str2 比较两个字符串str1和str2的大小 规则 按字典序 ASCII码序 如果str1和str2相等 函数值是0 如果str1大于str2 函数值是正整数 如果str1小于str2 函数值是负整数 6 字符串比较函数strcmp 如果str1和str2相等 函数值是0 如果str1大于str2 函数值是正整数 如果str1小于str2 函数值是负整数 staticchars1 20 china strcmp China China strcmp s1 china 正整数 负整数 0 strcmp s1 China include stdio h include string h main intres chars1 20 s2 20 gets s1 gets s2 res strcmp s1 s2 printf d res 输入 12345 输出 4 利用字符串比较函数比较字符串的大小strcmp str1 str2 为什么定义这样的函数 因为str1 str2str1 hello str1 str2是非法的 strcmp str1 str2 0 strcmp str1 hello 0 strcmp str1 str2 0 strlen str 计算字符串的有效长度 不包括 0 staticcharstr 20 Howareyou strlen hello 的值是 strlen str 的值是 7 字符串长度函数strlen 5 12 函数功能头文件puts str 输出字符串stdio hgets str 输入字符串 回车间隔 strcpy s1 s2 s2 s1strcat s1 s2 s1 s2 s1strcmp s1 s2 若s1 s2 函数值为0若s1 s2 函数值 0string h若s1 s2 函数值 0strlen str 计算字符串的有效长度 不包括 0 字符串处理函数小结 8 3 2字符串的指针表示 1 用字符数组表示字符串staticcharsa Thisisastring printf s sa printf s Hello 2 用字符指针表示字符串字符串是一个指针常量它的值就是该字符串的首地址 由于字符串是一个指针常量定义一个字符指针 接收字符串指针常量char sp Thisisastring printf s sp 8 3 2字符串的指针表示 staticcharsa Thisisastring printf s sa printf s Hello 1 使用字符指针进行字符串操作实现字符串复制函数strcpy s1 s2 8 3 3字符数组和字符指针 voidstrcpy chars1 chars2 inti for i 0 s2 i 0 i s1 i s2 i s1 i 0 voidstrcpy chars1 chars2 inti 0 while s1 i s2 i i 用字符指针实现字符串复制函数strcpy s1 s2 voidstrcpy chars1 chars2 inti 0 while s1 i s2 i i voidstrcpy char s1 char s2 while s1 s2 s1 s2 voidstrcpy char s1 char s2 while s1 s2 数组 改变下标指针 直接改变指针 实现字符串长度函数strlen str intstrlen charstr inti 0 while str i 0 i returni intstrlen char str char t str while str 0 str returnstr t 实现字符串比较函数strcmp s1 s2 intstrcmp chars1 chars2 inti for i 0 s1 i 0 i if s1 i s2 i break returns1 i s2 i strcmp char s1 char s2 for s1 0 s1 s2 if s1 s2 break return s1 s2 staticcharsa Thisisastring 字符数组sa由若干元素组成的 每个元素放一个字符 有确定的地址 char sp Thisisastring 字符指针是一个接收字符串首地址的变量 不能将字符串放到字符指针变量中去 在对指针赋值前 它的值是不确定的 2 字符指针和字符数组的区别 staticcharsa Thisisastring char sp Thisisastring staticcharsa 80 char sp 数组 指针常量 有确定的地址 指针 变量 strcpy sa Thisisastring sa Thisisastring sp Thisisastring include stdio h main staticcharsa string 设sa的值2000 char sp 设sp的值1fff printf nsa x sp x n sa sp sp sa printf sa x sp x n sa sp printf s sa printf s n sp 输出 2000 1fff2000 2000string string 例8 10 include stdio h main charsa 80 设sa的值2000 char sp Thisisastring 设sp的值1fff printf nsa x sp x n sa sp strcpy sa sp printf sa x sp x n sa sp printf s sp printf s n sp 输出 2000 1fff2000 1fffstring string 指针变量必须先定义 后使用 例如 char p scanf s p 可能出现难以预料的结果而char s str 20 s str scanf s s 是正确的 8 4指针数组和二级指针 8 4 1指针数组8 4 2二级指针8 4 3指针数组和字符串 inti inta 10 a是一个数组 它有10个元素 每个元素的类型都是整型 int p int pa 10 pa是一个数组 它有10个元素 每个元素的类型都是整型指针 8 4 1指针数组 inta 10 int pa 10 inti j k m int pa 4 pa 0 pa 0 pa 1 pa 2 pa 3 inti 1 j 2 k 3 m 4 int pa 4 for n 0 n 4 n printf x pa n for n 0 n 4 n printf d pa n inti 1 j 2 k 3 m 4 int pa 4 for pp pa pp pa 4 pp printf x pp pp pp pp for pp pa pp pa 4 pp printf x pp inti 1 j 2 k 3 m 4 int pa 4 如何定义pp int pp 8 4 2二级指针 指向指针的指针 main inta 10 int p a pp 输出 a 10 p 10 pp 10a 20 p 20 pp 20a 30 p 30 pp 30 main inta 10 b 80 int p 输出 a 10 b 80 p 10 pp 10a 10 b 80 p 80 pp 80 char name 4 Wang Li Zhao Jin 8 4 3指针数组和字符串 printf s n name 0 printf c name 0 输出 WangW char name 4 Wang Li Zhao Jin 输出 WangLiZhaoJin for i 0 i 4 i printf s name i for i 0 i 4 i printf c name i 输出 WLZJ 输入月 输出相应的英文名称 includemain intmonth scanf d 例8 11 prnname intm char name January Febr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论