C语言学习第七章(英文版)

上传人:飞*** 文档编号:33888628 上传时间:2018-02-18 格式:PPT 页数:122 大小:1.71MB
返回 下载 相关 举报
C语言学习第七章(英文版)_第1页
第1页 / 共122页
C语言学习第七章(英文版)_第2页
第2页 / 共122页
C语言学习第七章(英文版)_第3页
第3页 / 共122页
C语言学习第七章(英文版)_第4页
第4页 / 共122页
C语言学习第七章(英文版)_第5页
第5页 / 共122页
点击查看更多>>
资源描述

《C语言学习第七章(英文版)》由会员分享,可在线阅读,更多相关《C语言学习第七章(英文版)(122页珍藏版)》请在金锄头文库上搜索。

1、Chapter 7 Pointers,7.1Introduction7.2Pointer Variable Declarations and Initialization7.3Pointer Operators7.4Calling Functions by Reference7.5Pointer Expressions and Pointer Arithmetic 7.6Pointers and 1-D Arrays 7.7Pointers and 2-D Arrays 7.8Arrays of Pointers 7.9Pointers to Pointers,Outline,Pointer

2、Variable Declarations and InitializationPointer OperatorsCalling Functions by ReferencePointers and 1-D Arrays Pointers and 2-D Arrays,Key Points,7.1 Introduction,Pointers Powerful, but difficult to masterSimulate call-by-reference Close relationship with arrays and strings,7.1 Introduction,What is

3、a pointer ?How memory is organized in PC?A typical machine has an array of consecutively numbered or addressed memory cells that may be manipulated individually or in contiguous groups. A pointer is a group of cells (often two or four) that can hold an address.,7.1 Introduction,A variable in a progr

4、am is stored in a certain number of bytes at a particular memory location in the machine.int a;char ch;Each piece of memory should have a distinct number with it, named address.Can memory be accessed by its address?,FFC1,FFC2,FFC3,FFC4,FFC5,FFC6,a,ch,7.2 Pointer Variable Declarations and Initializat

5、ion,Pointer variablesContain memory addresses as their valuesNormal variables contain a specific value (direct reference)Pointers contain address of a variable that has a specific value (indirect reference)Indirection referencing a pointer value,7.2 Pointer Variable Declarations and Initialization,P

6、ointer declarations* used with pointer variablesint *myPtr; Declares a pointer to an int (pointer of type int *)Multiple pointers require using a * before each variable declarationint *myPtr1, *myPtr2;Can declare pointers to any data typeInitialize pointers to 0, NULL, or an address0 or NULL points

7、to nothing (NULL preferred),7.3 Pointer Operators, /* yPtr gets address of y */yPtr “points to” y,7.3 Pointer Operators,* (indirection/dereferencing operator)Returns a synonym/alias of what its operand points to*yptr returns y (because yptr points to y)* can be used for assignment Returns alias to a

8、n object*yptr = 7; /* changes y to 7 */Dereferenced pointer (operand of *) must be an lvalue (no constants)* and & are inverses They cancel each other out,7.3 Pointer Operators,Example int i; i = 7; int *p;/* declares p ,pointer to int.*/ p=,FFDE,7,7.3 Pointer Operators,* and ,i = 10, *p = 10 value

9、of p is FFDE,1. Declare variables2 Initialize variables3. PrintProgram Output,The address of a is 0012FF88The value of aPtr is 0012FF88 The value of a is 7The value of *aPtr is 7Proving that * and & are complements of each other.&*aPtr = 0012FF88*&aPtr = 0012FF88,7.3 Pointer Operators,The value of p

10、ointer can be changed during running period.It can be assigned to,Address of a normal variable int *p, *q, i, j; p = ,7.3 Pointer Operators,Change the pointer,int *p, i, j, k;i = 10; j = 20;k = 30;p = ,p,i,j,k,10,20,30,FFD1,FFD3,FFD5,FFD7,FFD3,FFD5,FFD7,102030,7.3 Pointer Operators-Initialization,In

11、itialize pointers to an address or 0.int i = 7;int *p = ,7.3 Pointer Operators- Pointers can not be pointed at,Do not point to an exact number of address.p = 4000; /*illegal*/Do not point at constants. &v /*illegal*/,7.3 Pointer Operators- Example of pointer assignment,int i, j, *p, *q;/* declaratio

12、n of pointers */i = 5; j = 6;p = /* error */,7.3 Pointer Operators- Example of pointer assignment,7.3 Pointer Operators,Exchange 2 numbers using pointers,void main() int *p1,*p2,*p,a,b,t; scanf(“%d,%d”,p1=, *p=*p1; *p1=*p2; *p2=*p; ,7.3 Pointer Operators-Demonstration,p1,p2,p,p1 = ,a,b,t,10,10,12,12

13、,10,10,12,7.3 Pointer Operators,Exchange 2 numbers using pointers in another way,void main() int *p1,*p2,*p,a,b; scanf(“%d,%d”,7.3 Pointer Operators-Demonstration,p1,p2,p,p1 = ,a,b,10,12,10,12,10,12,7.4 Calling Functions by Reference,Whenever variables are passed as arguments to a function, their va

14、lues are copied to the corresponding function parameters, and the variables themselves are not changed in the calling environment.This “call-by-value” mechanism is strictly adhered to in C.To change the values of variables in the calling environment, other language provide the “call-by-reference” me

15、chanism.,7.4 Calling Functions by Reference,In this section well see how the use of addresses of variables as arguments to functions can produce the effect of “call-by-reference”.For a function to effect “call-by-reference”, pointers must be used in the parameter list in the function definition.Then , when the function is called, addresses of variables must be passed as argument.,7.4 Calling Functions by Reference,Call by reference with pointer argumentsPass address of argument using *number used as nickname for the variable passed,7.4 Calling Functions by Reference,

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

当前位置:首页 > 资格认证/考试 > 其它考试类文档

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