C語言英文課件9指針_第1頁
C語言英文課件9指針_第2頁
C語言英文課件9指針_第3頁
C語言英文課件9指針_第4頁
C語言英文課件9指針_第5頁
已閱讀5頁,還剩52頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Chapter 9 Pointer(指針指針)9.1 The conception of the pointer and pointer variable 9.2 Pointer variable points to a variable 9.3 Pointer points to an array9.4 Pointer points to a string9.5 Function that return a pointer 9.6 Pointer arrays and the formal parameters of the main ()9.7 Pointer points to a fu

2、nctionpointer:The elite of C,import concept, import feature. Using pointer:1.can make program more compact,2.be care of using pointer9 . 1 The concept of the pointer and pointer variablePointer is addressaddress1. the address of the memory storage unit the serial number of the storage unit cf.data s

3、toring in the memory storage unit memory user data region200020012002200330103011.0000 00000000 01000000 00000000 0111.0000000011001100.2. Variable address and value20002002200420063010memory user data region.4279.2004.variable kvariable mvariable ivariable j variable i_pointer(1)variable address Th

4、e address of a variable is itself data that can be manipulated and stored in memory (2)valuecan be stored in memory(3)lvalueHow does the compiler process variable definition ? main() int num; scanf(%d,&num); printf(num=%dn, num); C compiler does operate as follows when it compile variable defini

5、tion statement ” int num”: (1) distribute variable memory space (2) login variable num to symbol tableeach variable in symbol table contains two attribute: 1) variable name(id) 2) its address (addr) variable address 3. The access of variable value(1)Direct access scanf(”%d”,&m); printf(”%d”, m);

6、 m=m+i;(2) indirect accessvisit variable value through another variable (a pointer variable)Example:define a pointer” i_pointer” i_pointer=&i ; scanf(“%d”,i_pointer); 20002002200420063010Memory user data region.4279.2004.Variable kVariable mVariable iVariable jVariable i_pointer9 . 2 Pointer var

7、iable points to a variable The definition of pointer variable general format: base-type *pointer-variable ; where:base-type is the type of the value to which the pointer points pointer-variable is the variable being declaredfor examples, int *p1; char *p2; using a pointer variable1. assignment of a

8、pointer variable value ( to assign an address to a pointer)for example, int *p1,*p2, i; p1=&i; p2=p1; p1i p2 2. to assign a value to a variable with pointer variable “*”:pointer operator、indirection operator is an unary operator(value-pointed-to)for example ,int i , j , *p; p=&i; *p=3; namel

9、y i=3; j=*p+2;ijpAfter definitionAfter p=&iafter *p=3:100010021004ijp100010021004ijp100010021004ijp1000 31000100010021004 after j=*p+2 3 51000Pointer variable must be assigned a value before it is used ,or it becomes a hanging pointer!operator & and *:(1)associativity :from right to left if:

10、int a, *p1; p1=&a; then: &*p1 =&(*p1)=&(a)=&a=p1 *&a =*(&a)=a(2)priority * prior to &3.”-”and “+”operator mostly apply to arrays pointer if :float x,*p1; p1=&x; if p=1000, then p+ is 1004 example: int a,b,*p; p=&a; a=3; b=5; (*p)+; the equivalence is a+ a=4 *p

11、+; the equivalence is *(p+) if p=1000, then p+ is 10029.1The indirect access of variables main() int a,b; int *pointer_1, *pointer_2; a=100;b=10; pointer_1=&a; /*to assign as address to pointer_1*/ pointer_2=&b;/* to assign bs address to pointer_2*/ printf (”d,dn”, a,b); printf(”d,dn”, *poin

12、ter_1,*pointer_2);); The results: 100, 10 100, 10&a&b10100pointer1b (*pointer2)a(*pointer1)pointer29.2Input two integers a and b ,output them ordered by value . main() int *p1,*p2,*p, a, b; scanf (dd,&a,&b);); p1=a; p2=b; if (ab) p=p1;pl=p2;p2=p; printf (na=d,b=dn”,a,b);); printf(max

13、=d,min=dn,*pl,*p2);); results: 5 9 a5,b9 max9,min5if(*p1*p2)&b&a95pp1p2ab&a&b95pp1p2abModify program as follows:main() int *p1,*p2,p, a, b; scanf (dd,&a,&b);); p1=a; p2=b; if (*p1*p2) p=*p1;*pl=*p2;*p2=p; printf (na=d,b=dn”,a,b);); printf(max=d,min=dn,*pl,*p2);); question:1.a

14、nalyses function result 5 9 a?,b? max?,min? 2.how about add three *before p?4.pointer variable act as function parameterquestion:can the swap function able to realize two variable exchange?void swap(int p1,int p2) int t; t=p1; p1=p2; p2=t;main() int a,b; printf(a,b=); scanf(%d%d,&a,&b); swap

15、(a,b); printf(a=%d,b=%dn,a,b);3636abp1p29.3 pointer variable act as function parametervoid swap(int *p1,int *p2) int t; t=*p1; *p1=*p2; *p2=t;main() int a,b; printf(a,b=); scanf(%d%d,&a,&b); swap(&a,&b); printf(a=%d,b=%dn,a,b);Transfer value(address)36ab&a&bp1p2Summary: We mu

16、st use pointer variable as function parameter to return the alternative value to calling function from called functionMechanism: When system execute the called function , if the formal parameter pointer variable changes, the value is saved via actual parameter at the end of the function call. 9.4 In

17、put three integer ,then output them in descending order that realized by using pointer as real parameter.void swap(int *p1, int *p2) int t; t=*p1, *p1=*p2, *p2=t; /*design a function to sort three numbers*/ void sort(int *p1,int *p2,int *p3) if( *p1 *p2 ) swap( p1, p2 ); if( *p1 *p3 ) swap( p1, p3 )

18、; if( *p2 *p3 ) swap( p2, p3 );main() int n1,n2,n3; printf(Input three numbers:); scanf(%d%d%d, &n1, &n2,&n3); sort(&n1,&n2,&n3); printf(sort numbers:%d,%d,%dn,n1,n2,n3); The results: Input three numbers: 2 1 3 sort numbers:3 2 19.3 Pointer points to an array1. summarizationc

19、onceptionint a5;elements pointer (address): a or &a0 a+1 or &a1 a+2 or &a2 a+3 or &a3 a+3 or &a4element value (name)a0 or *(a+0)a1 or *(a+1)a2 or *(a+2)a3 or *(a+3)a4 or *(a+4)Array s pointer (head address),array name a Declare a pointer of one-dimensional arrayelements pointer:

20、p or&p0p+1 or&p1p+2 or&p2p+3 or&p3p+3 or&p4Array element(name)p0 or *(p+0)p1 or *(p+1)p2 or *(p+2)p3 or *(p+3)p4 or *(p+4)instruction:int a5, *p=a (or&a0); namely: int a5, *p; pa;For example, int a5, *p=a (or *p=&a0); Quoting one-dimensional array element element value el

21、ement addressSubscript method: ai &ai visualPointer method : *(a+i) a+i *(p+i) p+i object program occupys memory fewer and run fast. The difference between p and a: a: address constant p: address variable 9.5 The quotation of array s elementsFirst:subscript methodmain() int a10, i; printf(“Input

22、 10 numbers: ”); for(i=0; i10; i+) scanf(“%d”, &ai); printf(“a10: ”); for(i=0; i10; i+) printf(“%d ”, ai); printf(“n”);results:Input 10 numbers: 0 1 2 3 4 5 6 7 8 9a10: 0 1 2 3 4 5 6 7 8 9Second :array namemain() int a10, i; printf(“Input 10 .: ”); for(i=0; i10; i+) scanf(“%d”, a+i); printf(“a10

23、: ”); for(i=0; i10; i+) printf(“%d”, *(a+i); printf(“n”); third:pointer variablemain() int a10, i, *p=a; printf(“Input 10 . : ”); for(i=0; i10; i+) scanf(“%d”, p+i); printf(“a10: ”); for(i=0; i10; i+) printf(“%d ”, *(p+i); printf(“n”); four:pointer variable +main() int a10, *p; printf(“Input 10 . :

24、”); for(p=a; pa+10; p+) scanf(“%d”, p); printf(“a10: ”); for(p=a; pa+10; p+) printf(“%d ”, *p); printf(“n”);pay attention to the difference between p+1 and p+! 2. Arrays act as parameters of function array name:formal parameter :receive actual parameters starting address actual parameter: Transfer t

25、he array s starting address to formal parameterWhen arrays and arrays pointers act as function parameters ,there are four equivalence forms(essentially one form ): (1)Both formal parameters and actual parameters are all array names. (2)Both formal parameters and actual parameters are all pointer var

26、iables. (3) Actual parameters are pointer variables,formal parameters are array names. (4)Formal parameters are pointer variables,actual parameters aree array names.3. Two-dimensional arrays pointera00 a01 a02 a03a10 a11 a12 a13a20 a21 a22 a231.1 3.2 5.1 7.32.5 4.6 7.8 8.84.5 6.7 3.9 2.6a0a1a2 aa+0a

27、+1a+2row pointersecondary pointercolumn pointerfirstlevel pointerTwo dimensional arrays pointer : example: int a34;discription:two_dimentional array has some features :array name “a”stands for the starting address and a row pointer that controls a line. a+i: row pointer ,points to line i *(a+i)or ai

28、: col pointer ,pointer to line i column 0 the format of how to access aij with pointer. aij *(ai+j) *(*(a+i)j) (*(a+i)janalyses:ai+j: column pointer points to array element aij.*(ai+j): the value of array element aij . If int a34, *p=a0; so p+1point to ? how to use p as pointer to access aij?4.Row p

29、ointer variablea pointer variable points to an one-dimensional array consists of N elementsdefinition form data type (* row pointer variable) n; notice:() indispensability,or it becomes pointer array (sixth section introduce).assign a value row pointer variable two dimensional array name| row pointe

30、r variable;9.6 Using row pointer and column pointer to output any elements in the two _dimentional array. using row pointer variable: main() int a34=1,2,3,4,5,6,7,8,9,10,11,12; int (*p)4, row, col; p=a; printf(“Input row = ”); scanf(“%d”, &row); printf(“Input col = ”); scanf(“%d”, &col); pri

31、ntf(“a%d%d = %dn”, row, col, *(*(p+row)+col); results: Input row = 1 Input col = 2 array12 = 7reflect:we can also use array name A as pointer, how to modify the program?using column pointermain() int a34=1,2,3,4,5,6,7,8,9,10,11,12; int *p, row, col; p=a0; printf(“Input row = ”); scanf(“%d”,&row)

32、; printf(“Input col = ”); scanf(“%d”,&col); printf(“a%d%d=%dn”, row,col, *(p+(row*4+col); Two-dimensional arrays pointer acts as function parameter There are two forms when two-dimensional arrays pointer acts as actual parameter ,column pointer and row pointer. Corresponding formal parameter mus

33、t use corresponding formal pointer variable:actual parameter : column pointer row pointer formal parameter:(column)pointer variable row pointer variable 5. The realization of the dynamic arraystatic array: The size of the array during the program executing can not be changed. disadvantage: waste the

34、 memory space if we can not to estimate the quantity of sata dynamic array: Specify the size of the array according to actual requirement during the program executing. In c language,we can realize the dynamic array by using arrays pointer act as array name and library functions that apply or release

35、 memory.dynamic arrays essence:one pointer points to an array9.7 The realization of the dynamic array#include “alloc.h”#include “stdlib.h”main() int *array=NULL, num, i; printf(“Input the number of element: ”); scanf(“%d”, &num); /*block memory used as application dynamic array */ array=(int *)m

36、alloc( sizeof(int) * num ); if ( array=NULL ) /* applying memory space is failed:prompt,exit.*/ printf(“out of memory, press any key to quit”); exit(0); /*exit():terminate running,return Operating System*/ printf(“Input %d elements: ”, num); /*prompt to input the number of integer*/ for (i=0; inum;

37、i+) scanf(“%d”, &arrayi); /*prompt to output the number of integer*/ printf(“%d elements are: ”, num);for (i=0; inum; i+) printf(“%d,”, arrayi); printf(“b ”); /* delete the last one data rear separator“,”*/ free(array);/*release the memory block from the application of malloc() function*/results

38、: Input the number of element: 3 Input 3 elements: 1 2 3 3 elements are: 1,2,3 program description:array=(int *)malloc( sizeof(int) * num ); statementmalloc() function and sizeof operatorlibrary function malloc()usage:void *malloc(unsigned size)function:distribute series space in the dynamic storage

39、 area of the memory which length id sizereturn value:if application is success ,return the starting address ,if not return NULL。function prototype alloc.h,stdlib.h。Malloc () returns a typeless pointer and it can points to any type data in order to void mistakes we must transfer the return value into

40、 the data type that points to .operator sizeofform:sizeof(variable name type name )function:calculate the space variabletype occupy.e.g,in IBM-PC sizeof(int)=2free(array);statement library function free()usage:void free(void *ptr)function:release the memory block ptr points to. return value:null In

41、principle,we use malloc () function to apply for MB and use free() function to release the MB after operation.If we release the MB out of time ,it may exhaust systemic memory resource soon , thereby the program can not run .reflect:(1)use sizeof(int) to calculate the memory byte number that int numb

42、er ocuppy , why not to use constant 2?(2)scanf(“%d”, &arrayi); printf(“%d,”, arrayi);Pointer points to array acts as array name it must be used according to the syntax rule of quoting array elements. (3) printf(“b ”);statement 9.4 character string pointer and pointer to character string characte

43、r string pointer : the character string s starting address 9.4.1 The denotation and quotation of character string character array quote character one by one character pointer variable whole quotequote character one by one9.8 use character pointer variable to express and quote character string .main(

44、) char *string=”I love China”; for(; *string!=0; string+) printf(“%c”, *string); printf(“n”); results: I love ChinaquoteexpressDescription :1、 char *string=I love China; definition and initialization definition and initialization string:string =the starting address of character contants amount to :c

45、har *string; string=“I love China”; string 2、 differ from character string char str =“I love China” str0 str7 str I l o v e C h i n a 0 I l o v e C h i n a 0string:character pointer variablestr=“I love China 錯誤錯誤2.Whole quotation9.9 use whole quotation and modify9.8。main() char *string=”I love China

46、”; printf(“%sn”,string); whole quote theorywhole quote theory:first of all output the first directional first of all output the first directional character character p o i n t e r a u t o m a t i c p l u s 1 p o i n t t h e n e x t c h a r a c t e r reduplicate the above process until counter with c

47、haracter string closing flag stringNotict :none but character arrays and can output the whole elements with one time by using array name3. The comarehe comare of the character pointer variable and character of the character pointer variable and character arrayarray( (1)memory contents differ(2)assig

48、n value mode differ(3)character pointer value could modify,but array name not I l o v e C h i n a 010.4.2 string pointer act as function parameterstring pointer act as function parameter 9.10 use function call mode and copy character stringfunction call mode and copy character stringvoid string_copy

49、(char *str_from, char *str_to) int i=0; for(; (*(str_to+i)=*(str_from+i)!=0; i+) ;/*loop body is blank statement*/ main() char array_str120=”I am a teacher.”; char array_str220; string_copy(array_str1, array_str2); /*array name is real parameter*/ printf(“array_str2=%sn”, array_str2); results: I am

50、a teacher. 9.5return pointer valued at functionreturn pointer valued at functionone function could return one intone function could return one int type type、float type、char type and can return one pointer type data return one pointer type data Format as follows:function type function type * * functi

51、on name (formal parameter table) function name (formal parameter table) 9.11 some logistic melt three term competition some logistic melt three term competition educate have got three individualeducate have got three individual,hunt thereinto at hunt thereinto at least has one term grade unfit out i

52、n. require that least has one term grade unfit out in. require that use pointer function realize.use pointer function realize./*/*seek() function:Estimation whether have got disqualification grade*/* formal parameterformal parameter: point to 3 int type elementary composition 1 point to 3 int type e

53、lementary composition 1 dimensional array line pointer variabledimensional array line pointer variable*/*return valuereturn value:(1) the one (column) pointer of the has disqualification gradethe one (column) pointer of the has disqualification grade,then return point to ones own profession begin co

54、lumnthen return point to ones own profession begin column; */* (2) have no disqualification gradehave no disqualification grade,return value for point to next row one return value for point to next row one (column) pointer(column) pointer*/*/int *seek( int (*pnt_row)3 ) int i=0, *pnt_col; /*define a

55、 column pointer pnt_col */ pnt_col=*(pnt_row+1); /*make pnt_col point to the head of next line (act as flag)*/ for(; i3; i+) if(*(*pnt_row+i)60) /*not passed*/ pnt_col=*pnt_row; /*make pnt_col point to head of current line */ break; /*break out of loop*/ return(pnt_col); /*main()()*/main() int grade

56、33=55,65,75,65,75,85,75,80,90; int i,j,*pointer; /*define a column pointer pointer */ for(i=0; i3; i+) /*control each student*/ pointer=seek(grade+i); /*use row pointer as real parameter,call seek() function*/ if(pointer=*(grade+i) /*this student at least have one course failed*/ /*output this stude

57、nts code and each grade*/ printf(“No.%d grade list: ”, i+1); for(j=0; j3; j+) printf(“%d ”,*(pointer+j); printf(“n”); program demonstrationresults:No.1 grade list: 55 65 75program description:(1) Main function.s pointer=seek(grade+i); statementWhen calling for seek() function,let the value of the re

58、al parameter grade+i(line pointer) ,be copied to formal parameter pnt_row(line pointer variable),make the formal parameter pnt_row point to grade array beta I row。(2)in pointer function seek() : 1) pnt_col=*(pnt_row+1); statement*(pnt_row+1) convert line pointer to column pointer,and point to Array

59、gradesbeta i+1 row beta 0 column,and assign a value to(column)pointer variable pnt_col .2) if(*(*pnt_row+i)60) row pnt_row is a row pointer,and point to Array grades i row ;*pnt_row make the pointer conversion from line to column, point to Array grades i row 0 column; and the value of the *pnt_row+j

60、 is still a pointer,it point to Arrays I row j column;*(*pnt_rowj) is a data( The value of Array element of gradeij )。Return9.6 pointer array and function main()s row referrow refer9.6.1 pointer Array 1. conception:conception: the each element of the array are one pointer datathe each element of the array are one pointer data。 design to design to point to multi-character stringpoint to multi-character string,approve lead string manipulation approve lead string manipulation all the more convenience,agilityall the more convenie

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論