bigC语言第12讲
《C语言程序设计》课后习题参考答案 李勃 邱晓红 主编 清华大学出版社

《C语言程序设计》课后习题参考答案第1章C语言及程序设计概述1.单选题(1)A (2)B (3)A (4)B (5)C2.填空题(1)//,/*…*/(2)scanf()(3)printf()3.判断题(1)对(2)错(3)错(4)对第二章:数据类型运算符与表达式1.单选题(1)C (2)D (3)C (4)B (5)C (6)A (7)B (8)D (9)A(8)D(9)A(10)B(11)C(12)C(13)D(15)B(16)C(17)A(18)A(19)B(20)C(21)C(22)D(23)C(24)A(25)D(26)B(27)C(28)B(29)B(30)A2.填空题(1)sqrt(pow(y,x)+log10(y)) (2)36(3)6(4)3 3(6)36(7)int x=8,y=8;(8)1(9)E(10)6,7,8,9(11)6(12)66,96(13)240(14)1,1,-1,-1(15)5,2,6(16)1(17)!(18)2(19)(a>0&&a<101)&&(a%3==0||a%7==0) (20)A3.程序分析题(1)2 72 74 94 412 12116(3)100 d 68 D97 a 49 1 (4)0 1 0 1 (5)2 4 6 7-0.58 7046.587.5(6)33 12 113 13 082 32(7)618 30181814.改错题(1)①无初始赋值,不能输出②数据精度丢失③少“;”号④单字符变量不能保存字符串常量⑤不能连续初始化⑥非法标识符(2)short i=38000溢出PI=3.1416 常量不能修改值Printf(“%d”,x%y) %必须是整数a*=(b+c)/=d符合赋值左边不能是表达式第三章:算法概念与顺序结构程序设计1.选择题(1)D(2)B、D(3)D(4)B(5)C(6)A(8)C(9)B(10)D2.填空题.(1)一条语句;(2)小于左右(3)%%(4)输出项列表输出控制符(5)取地址取a的地址(6)从盘获取一个字符(7)大括号(8)f=68.00(9)n1=%d\n n2=%d(10)7,5,c=33.程序分析题.(1)i=100,c=a,f=1.234000(2)65535,65536(10)1234,123.5,12345.5第四章:选择结构程序设计(1)C (2)B (3)B (4)B (5)D (6)D (7)D (8)A (9)B (10)A (11)B (12)B (13)D (14)A (15)C (16)A (17)C (18)D (19)B (20)D (21)A(23)A(24)A2.填空题.(1)1(2)5 8 8(3)4 5 99(4)2(5)10 20 0(6)2 1(7)-4(8)3(9)No(10)25(11)45 45(12)0(13)5.5(14)13(15)3第五章:循环结构程序设计(1)C(2)A(3)D(4)B(5)D(6)B(7)A A或B(8)输出4444(9)B(10)A(11)B(12)C(13)A(14)B2.填空题.(1)r=m;m=n;n=rm%n(2)3(3)-5(4)i%3==2&&i%5==3&&i%7==2 j==5j!=k(6)int s=8,j=i+2k+i+j==8(7)8(8)k&&i<=500 k/10 continue第六章:数组1.选择题(1)B(2)C(3)D(4)D(5)B(6)C(7)B(8)A(9)A(10)D2.填空题.(1)按行序优先的原则(2)0 9(4)1 2(5)6(6)前者以回车后者以空格或回车作为间隔符(7)gets()(8)&a[i](9)char k -1(10)9 83.程序分析题.(1)1 3 7 15(2)0 0 0(3)读取输入字符串中数字字符(4)①if(str1==str2) (strcmp(str1,str2)==0)②&c1[0]③m[i][k-1]第七章:函数的调用1.选择题(1)B(2)C(3)C(4)A(6)D(7)D(8)D(9)B(10)D(11)A(12)A(13)B(14)B2.填空题.(1)有参无参(2)顺序类型(3)定义调用(4)①fmax(a,N) ②s[k]=s[p](5)①age(n-1)+2 ②age(5)(6)①prt(c,n-1) ②prt(…‟,n-i) ③prt(…*‟,2*i-1) 3.程序分析题.(1)h l o(2)3(3)2 6(4)5 25。
C语言第12讲 结构体共用体

§6.3 结构体变量的引用
引用规则
结构体变量不能整体引用,只能引用变量成员
引用方式: 结构体变量名.成员名
例 struct 成员(分量)运算符 student 可以将一个结构体变量赋值给另一个结构体变量 例 struct student { stu1.num=10; int num; 优先级: 结构体嵌套时逐级引用 1 { int num; 例 struct student 结合性:从左向右 { char name[20]; int num; 例 struct name[20]; char sex; char student char name[20]; stu1.score=85.5; 例 structchar num; student { int sex; int char sex; age; printf(“%d,%s,%c,%d,%f,%s\n”,stu1); () if(stu1==stu2) { int age; num; stu1.birthday.month=12; char name[20]; float score; int …….. () stu1.score+=stu2.score; char score; name[20]; int age; struct float date char addr[30]; stu1.age++; float score; char addr[30]; stu1={101,“Wan Lin”,„M‟,19,87.5,“DaLian”}; () sex; { int char month; }stu1,stu2; addr[30]; char birthday int age; int }stu1,stu2; day; num name float score; }stu1,stu2; month day year int year; stu2=stu1; ( ) char addr[30]; }birthday; }stu1,stu2; }stu1,stu2;
biginteger用法c语言

biginteger用法c语言一、概述在C语言编程中,我们经常需要处理超过标准数据类型表示范围的大整数。
因为标准的整数类型(如i nt、l on g)有其表示范围限制,当处理超过这些范围的大整数时,我们不能简单地使用常规的整数运算符和函数。
针对这个问题,我们可以使用bi gi nt e ge r库来处理大整数。
本文将介绍b ig in te g er库的用法,包括如何安装和使用它来进行大整数运算。
二、安装b igintege r库在使用b ig in te ge r库之前,我们需要先安装它。
下面是安装步骤:1.打开终端或命令提示符,进入C语言项目的根目录。
2.下载bi gi nt eg er库的源代码,并将其解压到项目的文件夹中。
3.在终端或命令提示符中,使用gc c编译器编译b ig in te g er的源文件。
4.将生成的库文件链接到你的C语言项目中。
安装完成后,我们就可以开始使用bi gin t eg er库进行大整数运算了。
三、使用b igintege r库1.引入b i g i n t e g e r库在使用b ig in te ge r库之前,我们需要在代码中引入它。
下面是引入b i gi nt eg er库的代码示例:```c#i nc lu de"b ig in teg e r.h"```2.创建b i g i n t e g e r对象在使用bi gi nt eg er库之前,我们需要先创建一个bi gi nte g er对象。
下面是创建b ig in te g er对象的代码示例:```cb i nt nu m1,n um2;```3.给b i g i n t e g e r对象赋值我们可以使用`b in t_s et`函数将一个整数赋给一个b ig in te g er对象。
下面是给bi gi nt ege r对象赋值的代码示例:```cb i nt_s et(&nu m1,1234567890);b i nt_s et(&nu m2,-9876543210);```4.进行大整数运算使用bi gi nt eg er库,我们可以进行各种大整数运算,包括加法、减法、乘法和除法。
C#基础

目录第一章基础概念1第一讲C#基本概念第二讲熟悉VS窗口界面和简单编程第三讲变量第四讲值连符连接符占位符第五讲变量交换第六讲转义字符和算术运算符第七讲数据类型的转换第八讲复合运算符第九讲第六种数据类型—bool第十讲逻辑运算符第二章分支结构第十一讲If第十二讲If-else第十三讲If-else-if第十四讲switch-case第三章循环结构第十五讲While第十六讲do-while第十七讲调试第十八讲for第十九讲break、contimue跳转语句第四章基础概念2第二十讲常量/枚举第二十一讲结构第二十二讲数组第二十三讲冒泡排序第五章方法第二十四讲方法第二十五讲参数第二十六讲返回值第二十七讲方法重载第二十八讲三个高级参数第二十九讲方法的综合练习第一章顺序结构第一讲基本概念1、概念:.net与C#1) .net/dotnet :一般指.net Framwork框架,一种平台,一种技术。
2) C#(sharp):一种编程语言,可以开发基于.net平台的应用。
3) (*)java即使一种技术,又是一种语言。
2、.net都能干什么1) 开发桌面应用程序Winform(在桌面上不用输入网址就可打开的程序,比如QQ就是Winform)2) Internet应用程序(比如网站就是)3) 手机开发比如微软给诺基亚开发的WP7系统3、两种模式C/S 客户机(Client)/服务器模式(Server)B/S 浏览器(Browser)/服务器模式(Server)4、IDE(Integrated Development,集成开发环境)写程序的一款应用软件。
Visual Studio (简称VS,开发C#所用的一款软件)微软开发的一套完整的开发工具集,目前最新版本2010版本。
用于开发桌面应用程序、ASP应用程序、XML、Web Service.移动应用程序。
5、msdn微软软件的说明书,单独购买,是一个一visual studio和windows平台为核心整合的开发虚拟社区,包括技术文档、在线电子教程、网络虚拟实验室、微软产品下载等一系列服务。
新编英语语法教程1-18讲参考答案---上外第五版

《新编英语语法教程》1-18讲参考答案新编英语语法教程第01讲练习参考答案Ex. 1A1. A. his home workB. quickly, to play2. A. The huge black horseB. the race3. A. have thought aboutB. going into space4. A. warms up and crawlsB. out of the bag5. A. one of the most beautiful planets to look at through a telescopeB. because of the many rings that surround it6. A. 165 yearsB. to complete its path, or orbit,around the sun7. A. you and your brotherB. How many pairs of shorts8. A. the most expensive meal listed on the menuB. What9. A. an “Outdoor Code”B. their members10. A. can blowB. as fast as 180 miles (290 kilometers) an hour11. A. The spiral of heated air and moist airB. to twist and grow and spin12. A. The direction a hu rricane’s spiral movesB. counterclockwise13. A. does not shineB. At the north pole: for half of the year14. A. The cold winds that blow off of the Arctic OceanB. a very cold place15. A. might have beenB. guilty of murderEx. 1B1. SVCWithin the stricken area, not a single soul remained alive, and the city centre looked as if it had been razed by monster steam-roller.2. SVThe bomb exploded 1,000 ft. above the groun.3. SVOOn August 6, 1945, an American aircraft dropped a bomb on the Janpanese town of Hiroshima.4. SvoOThree days later, yet another bomb of the same kind gave the town of Nagasaki the same fatal blow.5. SVOCThe explosion made one and a half square miles of the city an expense of reddish rubble.6. SV AWithin the fraction of a second, the bomb changed from a metal cylinder into an immense mass of enpanding gas, millions of degrees hot.7. SVOAA tremendous blast of hot air whirled the debris of stone, cencrete, metal, and wood over the ground. Ex. 1C1. Walden Pond, once praised by Thoreau for its natural beauty, is now the site of many tourist stands.2. Almost every summer night the cooling northeast wind swept through our bedroom windows, marking air conditioning unnecessary and a light blanket welcome. / Swepping through our bedroom windows almost every summer night, the cooling northeast wind made…3. The steep surrounding slopes were capped with snow, which fed two streams plunging down to join in the valley below.4. With the river on one side and a large tree providing shade, this is a good spot for a picnic, and we can spread our blanket on the grassy knoll.5. Panting for breath after running up the stairs, Mr wood stood at his neighbour’s doo r and knocked again and again till someone opened it.6. The town folk envied horace, who had come into a small fortune with which he bought a big house and obtained a partnership in the biggest grocery in town.7. Standing in front of the mirror, Jim looked at his image, wondering at the big change that had come over him in recent years.8. The idea that his only daughter whom he had greatly wronged might never forgive him almost drove hime mad.9. The story, written in plain language, consists of three parts with an interesting plot centering round an aristocratic family living in 17th century France.10. Mud-covered and shivering, John sat hunched over a bowl of hot broth prepared by his father to drive off the chill.11. Far above the waters of a beautiful lake and over the tops of the tall pime trees growing on the steep ofa hill stand five Chinese-style pavilions.12. Farther down the street, the old man stopped and leaned against a lamp-post, listening to a cheerful song coming out of a restaurant on the oppsite side of the street.13. Sarah sank in the nearest chair, completely exhausted, her limbs stiff with cold, her mind a piece of blank.14. Throughout the day Mrs Rymer behaved very properly, her pleasant, refined face wearing a grave look, her elegant figure wrapped in deep mourning while occasionally she uttered a sigh or a sob.15. Tony thought it necessary to break the news to his family, that Mr Jacob, his former employer, had promised him a half-day job at 20 pounds a week. 16. The thought that he might have wronged his friend who had rendered him good services on many occasions troubled his mind, already overburdened with worries and cares.17. The men of the disbanded royal bodyguard, suddenly turned loose onto the street of a capital seething with unrest, unemployed and perhaps disgruntled at their abrupt dismissal, were a potentially dangerous element.18. For many years London has been a business centre with hotel accommadation for visiting businessmen toghter with well-to-do travellers but completely inadequate for the swarms of shour-stay tourists landing at Heathrow or disembarking at Dover.19. Nearing the top, he climbed recklessly faser and faster, his eyes already glowing with triumph, but suddenly he slipped and fell, tumbling to the ground and lying motionless there, a crumpled pile of arms and legs.20. Bertrand Russell was one of the very few persons who have received both the Order of Merit, which was conferred on him by the British government in 1949, and the Nobel Prize for literature, conferred in Norway in 1950.新编英语语法教程第02讲练习参考答案Ex. 2A1. come2. are3. has / have4. are5. are6. are7. was / were8. is9. costs 10. were 11. are 12. are 13. was 14. are 15. lie 16. were 17. are 18. is 19. is 20. was 21. Has 22. were 23. is 24. is 25. are 26. is 27. are 28. cover, are 29. is / are 30. was/wereEx. 2B1. were2. have3. is, is4. was5. were6. is7. is8. are9. is 10. are, are11. have 12. are, their, their 13. was, It, was 14, It / They, is / are 15. are, their, they, disapprove 16. were, they 17. was 18. are 19. were 20. were新编英语语法教程第03讲练习参考答案Ex. 3A1. is2. was3. is4. has5. were6. means7. is8. is9. is 10. is 11. were 12. sells 13. is 14. are 15. are 16. are 17. is 18. is 19. was 20.provides 21. are 22. was 23. stops 24. is 25. is 26. does 27. produces 28. is 29. is 30. was Ex. 3B1. ’s2. are3. is4. are5. is6. was7. is8. were9. is 10. ’s 11. is 12. is, is, is 13. are 14. is 15. have 16. is 17. are 18. looks 19. are 20. are 21. understand 22. has 23. was 24. have 25. was 26. was 27. are 28. is 29. is 30. was 31. is 32. is / are 33. leaves 34. is / are 35. are 36. are 37. is 38. comes 39. is 40. live 41. are 42. points / point 43. are 44. is 45. are 46. was 47. is / are 48. is 49. has 50. wasEx. 3C1. is / are2. is / are3. is4. is / are5. is / are6. remain7. is8. are9. has / have 10.care / cares 11. is 12. plays 13. is 14. am 15. are / is 16. was 17. is, has 18. was 19. come 20. is新编英语语法教程第04讲练习参考答案Ex. 4A1. description2. arrangement3. attendance4. peculiarity5. expectation6. argument7. dependence 8. originality 9. exaggeration10. measurement 11. purity 12. persistence 13. extension 14. statement 15. generosity 16. entrance 17. loneliness18. forgetfulness 19. happiness 20. seriousness Ex. 4B1. strange sounds2. foods are3. chief, tribes, their, salmon (s)4. The runners-up were given pound notes / The runners-up were each given a pound note.5. bodies, their heroes6. mice, tooth-marks7. Crises, occur, families8. / 9. / 10. these businesses 11. / 12. fruits are13. / 14. these articles are well written 15. / 16. several personal kindnesses17. / 18. sufferings 19. professors 20. children are playing, sandsEx. 4C1. experience2. waters3. for advice4. an important piece of information5. for it is fun6. were not numerous7. Poultry8. directors are9. militia 10. geniuses 11. merchandise has 12. sympathies 13. experiences 14. were times, rivalry 15. clippings, were 16. lookers-on 17. foliage is 18. photos 19. 12-pages 20. Luggage isEx. 4D1. blade2. piece3. choir4. flash5. lump6. gang7. grain8. head9. staff 10. collection 11. fleet 12. cluster 13. bundle 14. bunch 15. team 16. piece 17. ear 18. bar 19. herd 20. pack 21. flock 22. crowd / swarm 23. troupe 24. bench 25. grove 26. collection 27. suite 28. squadron 29. band 30. libraryEx. 4E1-5 BCADC 6-10 CBDAB 11-15 ADBBC 16-20 BDCAB新编英语语法教程第05讲练习参考答案Ex. 5A1. my father has a car2. the bull has horns3. the prisoner escaped4. her parents consented5. somebody released the prisoner6. somebody assassinated the President7. a letter from the general / the general sent a letter8. the crowd felt sympathy9. a college for women10. a summer day, a day in the summer11. the earth has a (rough) surface12. the absence lasted ten days13. a doctoral degree, a doctorate14. the bird made the nest15. the committee made a report16. a story told by the girl / the girl told a story17. the volcano erupted18. the victim had courage / the victim was courageous19. somebody punished the boy20. the critics recevied the play in a hostile manner Ex. 5B1. The comedian performed, and he was well received by a huge audience.这位喜剧演员的演出很受广大观众的欢迎。
Unit 12

Unit 12 What did you do last weekend ?第一课时Section A (1a~2d)自主预习案学生自学新单词(教材P67-68),比一比,看谁记得又快又准。
1.camp v.扎营;搭帐篷ke n.湖;湖泊3.beach n.海滩;沙滩4.sheep n.绵羊5.as prep.&adv.作为;当作6.natural adj.自然的7.visitor n.游客;访问者8.tired adj.疲倦的;疲劳的9.stay v.停留;待学生自查并写出本课短语(教材P67-68),读一读,记一记。
1.go to the cinema 去看电影2.go boating 去划船3.go to the beach 去海滩4.play badminton 打羽毛球5.living habit 生活习性6.stay up late 深夜不睡;熬夜湖边野营Step 1 情景导入Teacher:How was your last weekend?Student1:It was very good.Teacher:What did you do last weekend?Student 1:I milked the cows.Teacher:What did you do last weekend?Student 2:I visited my grandmother.…环节说明:由师生问答的形式直接引出本节课要学的语言目标一般过去时态,简单明了,直奔主题。
Step 2 完成教材1a-1c 的任务1.教师领读1a中的单词或词组,学生跟读并且识记,然后两人一组互相提问。
2.认真观察图片,将图片和1a中所给的短语匹配,集体核对答案,完成1a。
3.大声朗读图片中的小对话,为听力训练做好准备。
4.认真听录音,根据听力内容在每张图片上填上事情发生的时间morning, afternoon和evening。
DSP 第12讲 DMA控制器

§12.4 DMA存储器映射
VC5402不能访 问程序空间和I/O 空间,数据空间 也仅能访问一部 分,且不受 MP/MC、DROM 和OVLY等控制 位的影响。每个 16 bit的DMA数 据的传输需要一 先读后写相继的 操作;VC5402 仅支持片内 DATA区的数据 传输,需要4个时 钟周期。
7~6
INTOSEL
0
5~0
DE[5:0]
0
§12.2.2 DMA通道优先级和使能控制寄存器(DMPREC)
DMA中断信号与其它片上外设的中断信号复用相同的中 断向量位置,可以节约中断资源占用的寄存器存储区域
表 VC5402的DMA通道中断向量表 (97页,88页)
INTOSEL[1:0]值
IMR/IFR# 00b 01b 10b 11b
第12讲 直接存储器访问 (DMA)控制器
§12.1 DMA概述
DMA:是指在不需要CPU干预的情况下在映射存 储区的不同区域间(程序,数据或I/O空间)传输 数据的技术。 术语:
读传输:DMA从存储器的源位置读取一个数据。 写传输:DMA写一个读传输期间所读的数据到存储器的 目标位置。 数据单元传输:一个数据的读和写传输的组合。 帧传输:每个DMA通道传输的每一帧有多少个数据均是 可独立编程的。在完成一帧传输的过程中,DMA会移动 一帧中所有的数据元素。 块传输:每个DMA通道传输的每个块有多少个帧均也是 可独立编程的。在完成一块传输的过程中,DMA会移动 定义在块中的所有的帧。
§12.5 DMA应用实例
7
10 11
计时器1中断
McBSP1接收数据中断 McBSP1发送数据中断
计时器1中断
Unit2Lesson12Maddy'sFamilyPhotos(教学设计)冀教版英语五年级上册

Lesson 12 Maddy’s Family Photos冀教版学校英语五班级上册其次单元一、教学背景冀教版学校英语是从三班级为起始版到学校六班级毕业共有8册书。
这套教材的编排以及所选内容无论从家庭、学校还是社会都比拟贴近同学的生活,而且内容的选择具有较强的有用性,表达了人文特色。
冀教版学校英语教材每课都配有与课文相配套的图片,使同学对课文内容理解起来更加通俗易懂。
配的测试题有助于检测同学对所学内容的把握程度,使老师能准时调整教学方法,转变教学策略,以到达最正确的教学目的。
所配歌曲,既能活泼课堂气氛,使同学轻松学习,又能稳固所学学问。
教学内容由浅入深,逐步推动,渐渐渗透。
本册教材分为四个单元,共28课时,每单元的最终都是一个小测试,对本单元的重点句型及词汇进行复习。
每一课都有相应的练习,对本课的重点词汇和句型进行了归纳,这种编排符合学校生学习和记忆的特点,具有很强的科学性和规律性。
本册书内容,留意同学英语听说读写力量的培育和训练,教学中支配了大量的,好玩的教学活动,引导同学在轻松开心,乐观向上的气氛中学习英语,使英语学习变得“既简洁又好玩〞。
本课文位于冀教版五班级上册Unit 2 My country and Englishspeaking countries倒数其次课,本节课为新授课,通过居住在不同国家Maddy家人的照片,了解到Maddy家人居住的地点、所说的语言以及他们宠爱做的事,引导同学运用英语进行介绍和沟通。
其次单元是熟悉讲英语的主要国家,学习一些世界闻名的景点和地点,学会用英语简洁介绍中国,英国,,加拿大,澳大利亚等国家的特点和习惯。
培育同学敬重各国人民的传统风俗和习惯,懂得国家与国家之间的文化差异,懂得利用所学和他人相处。
本节课的授课对象是五班级同学,五班级的同学已经具备了肯定的听说读写的力量。
课堂上也根本能听懂老师课堂上的提问。
依据图片提示能听懂文本内容。
学习本课文之前,同学已把握了中国、加拿大、、英国、加拿大、澳大利亚国家的地图、语言、首都、国旗及一些其他闻名代表特色。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
WANGJINLING
指针是特殊类型的变量,其内容是变 合 量的地址。在使用前必须说明,说明某标 肥 识符是指针类型,并可指向某种类型的变 工 量或数据结构。
业 大 学
1、指针变量的定义 格式: type *pname1,…*pnamen;
标识符命名的指 针变量名
WANGJINLING
合 肥 指针指向对象的类型 工 int *p, *q; /* p、q是指向整型变量的指针业 */ 大 float *pfValue ,*pf; 学 /* pfValue和pf是指向浮点型的指针 */
类型长度
p1++; /*指向a后的整型单元*/ p1- -; /*指向a前的整型单元*/ p1+n; /*指向a后的n个整型单元*/
6.2 指针与数组
数组中的各元素按下标的顺序占据一段连续 的内存空间,数组名是这段连续内存的首地址, 指针访问数组元素非常方便。
WANGJINLING
6.2.1 指针与一维数组 通过指针引用数组元素有三个步骤:
指针标志
2、指针变量的赋值
① int a, *p=&a;
WANGJINLING
初始化
② int a=10,*p,*q; 注:*p=&a p=&a; 赋值语句 q=p; p=NULL
合 /*Error!*/ 肥 工 业 大 学
例:
#include <stdio.h> void main (void ) { int x ,*p; x=55; p=&x; printf ( “ %d, %u ”, x, *p) ; *p=65; printf ( “ %d, %u”, x, *p) ; }
WANGJINLING
合 肥 工 注 数组名是常量, 业 意:不能赋值! 大 a=p; /* Error! */ 学
举例:用三种方法输出数组中的奇数( 与P175 / p168 例6.8比较 )
#include <stdio.h> void main(void ) { int i ,a [10] ;,*p ; p = a; 循环输入 for (i=0 ; i<10 ; i++ ) scanf ( “ %d” , &a[i] ) ; a+i p++ p= a; \* p=&a[0]; 调整指针位置 *\ 循环判断,满足条件输出。 for (i=0 ; i<10 ; i++) *(p+i) *(a+i) *(a+i) if ( *(p+i) % 2 ) printf ( “ %d”, a[ i ]); ) a[ i ] }
WANGJINLING
数组元素法。 数组名法。 指针法。
合 肥 工 业 注意指针在运算时的变化。 结果是否 大 正确? 学
★ 课堂作业:
分析下列程序运行结果 (假设变量a,b的地址分 别为20H,30H,画出程序 执行后的内存示意图 。 见P201 / p191习题4)。
与P170 / p162 例6.3比较,要实 现两个数交换, 如何修改程序??
1 5 9
2 6 10
3 7 11
(a[0])[4] 8 (a[1])[4] 12 (a[2])[4]
4
合 肥 工 业 大 学
⑴ 将a理解为一维数组: 数组有三个元素,它们分别 为a[0]、 a[1],a[2] 。各个元素又是一个有四个元 素的一维数组。 ⑵ 行地址(二级指针) ⑶ 列地址(一级)
合 输出p的值(a的地址)。 肥 输出p的地址。 工 a=5; 业 大 a++; 学
scanf(“%d”, &a);
注意:
⑴ 指针必须指向对象后, int *p; *p=2; 才能引用。 /* Error! */ ⑵ & 和 * 为互补运算。
WANGJINLING
int a,*p; p=&a; 则:&*p p *&a a
第六章 指 针
6.1 指针的基本概念
指针是一种特殊的数据类型,是C语言 合 的重要特征,是C语言访问内存数据和程序 肥 的一种灵活有效的手段。它可以直接处理内 工 存地址,动态分配内存,简洁表达复杂的数 业 据结构。使程序简洁、高效、紧凑。可以说, 大 指针是C语言的精髓。
WANGJINLING
学
指针涉及数据的物理存储,概念复杂,
*(a+i )=a[i]=&a[i][0]
合 a[1][0]=*a[1]=**(a+1) 肥 a[2][0]=*a[2]=**(a+2) 工 a[i][0] =*a[i] =**(a+i) 业 大 学
数组名地址的两级管理: a a[i]=*(a+i) a[i][0]=*a[i]=**(a+i)
使用灵活且容易出错并不宜查错,所以
WANGJINLING
合 较难掌握。学习指针要抓住基本的概念. 肥 简 单 地 说 , 指 针 == 地 址 , 工 指针变量==存放地址的变量. 业 大 学
6.1.1 内存、地址、指针 在计算机中所有的数据(包括程序和程 序正在使用的数据)都存放在内存中,内 存的基本单元是字节(Byte)。
为了访问内存单元,进行数据的存取,CPU 合 给每个内存单元一个编号,该编号称为内存单 元的地址。 肥
WANGJINLING
工 当说明变量时,系统将在内存中为其 分配相应的内存单元。由此确定变量的业 地址及内存中的存储方式—地址的分配大 由C编译系统决定,通过地址运算符&可 学 以获得。
int a=2;
⑴ 说明指针和数组 ⑵ 指针指向数组
合 肥 int *p,a[10]; p=a; 工 /*指向数组的首地址*/ 业 p=&a[0]; 大 /*指向数组的首地址*/ 学
⑶ 通过指针引用数组元素 当指针指向数组的首地址时,则下标为i的 元素地址为: p+i 或a+i 引用数组元素可以有三种方法: 下标法: a[ i ] 指针法: *(p+i) 或 p[ i ] 数组名(地址)法:*(a+i)
...
x
55 65 ...
2000H
WANGJINLING
p 2000H
合 肥 工 业 大 学
3、指针变量的引用 目的是通过指针引用内存对象(变量、数组 元素等). 指针操作运算符:
& 取地址运算符 &a 表示取变量a地址的运算
WANGJINLING
合 肥 指针的引用按三个步骤进行: 工 int a,*p; 2000H a 2 ⑴ 说明指针 业 p=&a; ⑵ 指针指向对象 *p=2; 大 p 2000H ⑶ 通过指针引用对象 学
按 a[0] 行 #include<stdio.h> a[1] 递 a void main() 增 { a[2]
a[0][0]
a[0][1]
a[0][2]
a[1][0]
a[2][0]
a[1][1]
a[2][1]
a[1][2]
a[2][2]
WANGJINLING
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; printf(“%u,%u,%u,%u\n”,a,*a,&a[0],&a[0][0]); 数组名是数组的地址,而且是常量,* 运算不改变其值! printf(“%u,%u,%u,%u\n”,a+1,&a[1][0],*a+1,&a[0][1]); 故a和*a、(a+i)和 a+1,*(a+1),a[1],&a[1]); printf(“%u,%u,%u,%u\n”, *(a+i) 是相等的(级别不等)。 printf(“%d,%d,%d,%d,%d\n”, *(*(a+1)+2),*(a[1]+2),a[1][2],*a[1],**(a+1)); }三种地址等价:
WANGJINLING
6.2.2 指针与二维数组
1、二维数组的地址 如:int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
其二维结构为(按行优先转换为一维数组存放在内存):
WANGJINLING
列 为了便于索引,C语言将数组分为两级管理。
a[0] 行 a[1] a[2]
a =&a[0]=&a[0][0] *a=a[0]=&a[0][0] *(a+1)=a[1]=&a[1][0] a+1=&a[1]=&a[1][0]
⑷ 元素
a[0][0]=*a[0]=**a
WANGJINLING
a+2=&a[2]=&a[2][0]
a+i =&a[i] =&a[i][0]
*(a+2)=a[2]=&a[2][0]
#include <stdio.h> void main ( void) { int x, *p, **q ; x=10; p为指向整型的指针。 p=&x; q为指向整型指针的指针。 q=&p; printf (“%d” , * *q) ; }
WANGJINLING
x p
q
合 2000H 2000H 肥 工 3000H 3000H 业 大 学
WANGJINLING
a b
2 3 3
&a 2000H &b
3 5
2000H p1 2400H
p2 2400H
合 肥 2400H 工 业 大 学