【正文】
g a i n \0 s t r o n g e r \0 p o i n t \0 m u c h \0 name[0] name[1] name[2] name[3] name[4] b y e \0 g a i n \0 s t r o n g e r \0 p o i n t \0 m u c h \0 b y e \0 ?二維數(shù)組與指針數(shù)組區(qū)別 : 二維數(shù)組存儲空間固定 字符指針數(shù)組相當(dāng)于 可變列長 的二維數(shù)組 分配內(nèi)存單元 =數(shù)組維數(shù) *2+各字符串長度 指針數(shù)組元素的作用相當(dāng)于二維數(shù)組的行名。 …….. } i s p \ r t r n \ s i c \ ] ] ] ] C語言程序設(shè)計 char name[5][9]={“gain”,“much”,“stronger”, “point”,“bye”}。 p[3]=NULL。 p[1]= Lisp。 …….. } 或 : void main() { char *p[4]。 p[2]=c。 p[0]=a。 char c[]=Basic。 …….. } int *pb[2] pb[0] pb[1] int b[2][3] 1 2 3 2 4 6 C語言程序設(shè)計 ?指針數(shù)組賦值與初始化 L i s p \0 F o r t r a n \0 B a s i c \0 p[0] p[1] p[2] p[3] 0 賦值 : void main() { char a[]=Fortran。 pb[1]=b[1]。 指針?biāo)赶蜃兞康臄?shù)據(jù)類型 指針本身的存儲類型 區(qū)分 int *p[4]與 int (*p)[4] ?指針數(shù)組賦值與初始化 賦值 : main() { int b[2][3],*pb[2]。 printf(%d\n,*p)。 int *p。y。x。 printf(%d\n,*p)。 int *p。y。x。 printf(%d\n,*p)。a,amp。 int *p。 else return y。 printf(%d\n,*p)。a, amp。 int *p。 else return y。 return(pt)。 } float *search(float (*pointer)[4], int n) { float *pt。i4。 p=search(score,m)。m)。 printf(Enter the number of student:)。 float *search(float (*pointer)[4],int n), *p。 } C語言程序設(shè)計 ? 返回指針值的函數(shù) ? 函數(shù)定義形式: 類型標(biāo)識符 *函數(shù)名 (參數(shù)表 )。 } add(int x,int y) { printf(“sum=”)。 } min(int x,int y) { printf(“min=”)。 } max(int x,int y) { printf(“max=”)。 result=(*fun)(x,y)。 process(a,b,add)。 process(a,b,max)。a,amp。 void process(int,int,int (*fun)())。 return(z)。 if(xy) z=x。 printf(a=%d,b=%d,max=%d\n,a,b,c)。b)。 scanf(%d,%d,amp。 int a,b,c。 return(z)。 if(xy) z=x。 printf(a=%d,b=%d,max=%d\n,a,b,c)。b)。 scanf(%d,%d,amp。 ?對函數(shù)指針變量 p?n, p++, p無意義 ( )不能省 int (*p)() 與 int *p()不同 C語言程序設(shè)計 例 用函數(shù)指針變量調(diào)用函數(shù),比較兩個數(shù)大小 main() { int max(int ,int)。 ? c=(*p)(a,b)。 如 int (*p)()。 C語言程序設(shè)計 ? 指針與函數(shù) ?函數(shù)指針:函數(shù)在編譯時被分配的入口地址 ,用函數(shù)名表示 max …... 指令 1 指令 2 ?函數(shù)指針變量賦值 :如 p=max。 gets(str)。 (?) scanf(“%s”,str)。 (?) a={1,2,3,4,5}。 str=“Hello”。 (?) char str[10],*cp。 (?) int a[]={1,2,3,4,5}。 (?) char str[]={?H?,?e?,?l?,?l?,?o?,?!?}。 (?) C語言程序設(shè)計 ?字符串與數(shù)組關(guān)系 ?字符串用一維字符數(shù)組存放 ?字符數(shù)組具有一維數(shù)組的所有特點 ? 數(shù)組名是指向數(shù)組首地址的地址常量 ? 數(shù)組元素的引用方法可用指針法和下標(biāo)法 ? 數(shù)組名作函數(shù)參數(shù)是地址傳遞等 ?區(qū)別 ? 存儲格式:字符串結(jié)束標(biāo)志 ? 賦值方式與初始化 ? 輸入輸出方式: %s %c char str[]={“Hello!”}。 cp=str。 scanf(“%s”, cp)。 scanf(“%s”,str)。 cp=“I love China!”。 str=“I love China!”。 與 char str[20]。 printf(\nstring_a=%s\nstring_b=%s\n,a,b)。 printf(string_a=%s\nstring_b=%s\n,a,b)。 } void main() { char *a=I am a teacher.。 *to=39。\039。 } void copy_string(char *from,char *to) { for(。 copy_string(a,b)。 char b[]=You are a student.。\039。 i++。\039。 string *string!=0 C語言程序設(shè)計 ?字符串指針作函數(shù)參數(shù) 例 用函數(shù)調(diào)用實現(xiàn)字符串復(fù)制 ( 1)用字符數(shù)組作參數(shù) ( 2)用字符指針變量作參數(shù) a I a m a t e a c e h \0 r . from a b y u a r a s u t n d e to b o e t . \0 I a a e c e h \0 r . t . \0 m t a void copy_string(char from[],char to[]) { int i=0。 } } I l o v e C h i string n ! a \0 字符指針 初始化 :把字符串 首地址 賦給 string ? char *string。 while(*string) { putchar(string[0])。 printf(“%s\n”,string)。 printf(“%s\n”,string+7)。 ?二維數(shù)組名是一個指向有 10個元素的一維數(shù)組的指針常量 ?p=a+i 使 p指向二維數(shù)組的第 i行 ?*(*(p+i)+j) ? a[i][j] ?二維數(shù)組形參實際上是一維數(shù)組指針變量 ? 即 int x[ ][10] ? int (*x)[10] ?變量定義 (不是形參)時兩者不等價 ?系統(tǒng)只給 p分配能保存一個指針值的內(nèi)存區(qū) (一般2字節(jié));而給 a分配 2*5*10字節(jié)的內(nèi)存區(qū) C語言程序設(shè)計 ?指針與字符串 ?字符串表示形式 ?用字符數(shù)組實現(xiàn) 例 void main( ) { char string[]=“I love China!”。 search(score,3)。 } } } void main() { void search(float (*p)[4], int n)。i++) printf(% ,*(*(p+j)+i))。 for(i=0。i++) if(*(*(p+j)+i)60) flag=1。 for(i=0。jn。 } 列指針 行指針 函數(shù)說明 float p[][4] 65 52 79 60 80 87 90 81 90 99 100 98 p p ? p[n][i] C語言程序設(shè)計 例 3個學(xué)生各學(xué) 4門課,計算總平均分,并查找一門以上課 不及格學(xué)生, 輸出其各門課 成績 void search(float (*p)[4], int n) { int i,j,flag。i4。 printf( No.%d :\n,n)。 printf(average=%\n,aver)。p++) sum=sum+(*p)。 for(。 } void average(float *p,int n) { float *p_end, sum=0,aver。 average(*score,12)。 void search(float (*p)[4],int n)。 int *p2=a[0]。 } 運行結(jié)果: 2, 4, 7, 5, 3 1 2 3 4 3 4 5 6 5 6 7 8 p q 2p q p q C語言程序設(shè)計 ?二維數(shù)組的指針作函數(shù)參數(shù) ?用指向變量的指針變量 ?用指向一維數(shù)組的指針變量 ?用二維數(shù)組名 實參 形參 數(shù)組名 int x[][4] 指針變量 int (*q)[4] 數(shù)組名 int x[][4] 指針變量 int (*q)[4] 數(shù)組名 a 數(shù)組名 a 指針變量 p1 指針變量 p1 若 int a[3][4]。i++) printf(%d,a[i][i])。 } for(i=0。i++) { if(i==0) (*p)[i+i/2]=*q+1。 for(i=0。 int i。 int a[3][4]。 p=amp。 p=amp。 p=a[0]。 p=amp。 p=amp。 } p=a[0]。j++) printf(%d ,*(*p+j))。i++,p++) for(j=0。 for(p=a,i=0。 a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] a[0][2] a[0][3] a[1][2] a[1][3] a[2][2] a[2][3] a a+1 a+2 p p+1 p+2 p[0]+1或 *p+1 p[1]+2或 *(p+1)+2 *(*p+1)或 (*p)[1] *(*(p+1)+2) 一維數(shù)組指針變量維數(shù)和 二維數(shù)組列數(shù) 必須相同 C語言程序設(shè)計 例 一維數(shù)組指針變量舉例 void main() { static int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23}。 ( )不能少 int (*p)[4]與 int *p[4]不同 p的值是一維數(shù)組的 首地址, p是 行指針 ?可讓 p指向二維數(shù)組某一行 如 i