C
C 料, 數, - 列 串 理
列 main 數串列
什 pointer) 數, 數, 數 數 省 不 不,
數 (1) 數, 不 數 * 料 * 數 int *int_ptr; char *ch_ptr; float *float_ptr; double *double_ptr;
數 (2) int i=3; int *ptr; ptr=&i; 1000 1012 ptr 數, 數 1004 1008 1012 3 i 數, 3, *ptr
(1) & 來 數 & 數 1000 1012 ptr 數 &i 數 int i=3; int *ptr; 1004 ptr=&i; 1008 1012 3 *ptr i 數
(2) * 來 * 數 int i; int *p; p=&i; *p=50 1000 1004 1008 1012 1000 1004 1008 1012 1012?? 1012 50
練 料 數 32 4 bytes 數.
(1) :int *p=10; : int x=10; int *p=&x; Int *p = (int *)10; 數 int x = 10; 數 int *x = (int *)10; &x 10 x x 10 *x
(2) 數 數 1 料 來 1 1 便 料類 料 例 不
(3) 數 數 例
(4) 兩 料 數 兩 料 料 例
(1) call by value call by address
Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved. 1 /* Fig. 7.6: fig07_06.c 2 Cube a variable using call-by-value */ 3 #include <stdio.h> 4 5 int cubebyvalue( int n ); /* prototype */ 6 7 int main() 8 { 9 int number = 5; /* initialize number */ 10 11 printf( "The original value of number is %d", number ); 12 13 /* pass number by value to cubebyvalue */ 14 number = cubebyvalue( number ); 15 16 printf( "\nthe new value of number is %d\n", number ); 17 18 return 0; /* indicates successful termination */ 19 20 } /* end main */ 21 22 /* calculate and return cube of integer argument */ 23 int cubebyvalue( int n ) 24 { 25 return n * n * n; /* cube local variable n and return result */ 26 27 } /* end function cubebyvalue */
The original value of number is 5 The new value of number is 125 Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved.
Before main calls cubebyvalue : int main() { int number = 5 ; number=cubebyvalue(number); } number 5 int cubebyvalue( int n ) { return n * n * n; } n undefined After cubebyvalue receives the call: int main() { int number = 5 ; number = cubebyvalue( number ); } number 5 int cubebyvalue( int n ) { return n * n * n; } n 5 After cubebyvalue cubes parameter n and before cubebyvalue returns to main : int main() { int number = 5 ; number = cubebyvalue( number ); } number 5 int cubebyvalue( int n ) { 125 return n * n * n; } n 5 Analysis of a typical call-by-value. (Part 1 of 2.)
After cubebyvalue returns to main and before assigning the result to number : int main() number { int number = 5; 5 125 number = cubebyvalue( number ); } int cubebyvalue( int n ) { return n * n * n; } n undefined After main completes the assignment to number : int main() { int number = 5; 125 125 number = cubebyvalue( number ); } number 125 int cubebyvalue( int n ) { return n * n * n; } n undefined Analysis of a typical call-by-value. (Part 2 of 2.)
Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved. 1 /* Fig. 7.7: fig07_07.c 2 Cube a variable using call-by-reference with a pointer argument */ 3 4 #include <stdio.h> 5 6 void cubebyreference( int *nptr ); /* prototype */ 7 8 int main() 9 { 10 int number = 5; /* initialize number */ 11 12 printf( "The original value of number is %d", number ); 13 14 /* pass address of number to cubebyreference */ 15 cubebyreference( &number ); 16 17 printf( "\nthe new value of number is %d\n", number ); 18 19 return 0; /* indicates successful termination */ 20 21 } /* end main */ 22 23 /* calculate cube of *nptr; modifies variable number in main */ 24 void cubebyreference( int *nptr ) 25 { 26 *nptr = *nptr * *nptr * *nptr; /* cube *nptr */ 27 } /* end function cubebyreference */ 數 數
The original value of number is 5 The new value of number is 125 Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved.
Before main calls cubebyreference : int main() { int number = 5; } number cubebyreference( &number ); 5 void cubebyreference( int *nptr ) { *nptr = *nptr * *nptr * *nptr; } nptr undefined After cubebyreference receives the call and before *nptr int main() { int number = 5; } number cubebyreference( &number ); 5 is cubed: void cubebyreference( int *nptr ) { *nptr = *nptr * *nptr * *nptr; } nptr call establishes this pointer After *nptr is cubed and before program control returns to main : int main() { int number = 5; } number 125 cubebyreference( &number ); void cubebyreference( int *nptr ) { 125 *nptr = *nptr * *nptr * *nptr; } called function modifies nptr caller s variable Analysis of a typical call-by-reference with a pointer argument.
練 SWAP #include<stdio.h> void swap(int *,int *); int main() { int i,j; } i=1,j=2; swap(&i,&j); printf("%d %d",i,j); void swap(int *p,int *q) { int temp; } temp=*p,*p=*q;*q=temp;
(2)
列 int array[10]; 列 array 列, array+1., int *ptr; ptr, ptr+1. 列.
Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved. 1 /* Fig. 7.20: fig07_20.cpp 2 Using subscripting and pointer notations with arrays */ 3 4 #include <stdio.h> 5 6 int main() 7 { 8 int b[] = { 10, 20, 30, 40 }; /* initialize array b */ 9 int *bptr = b; /* set bptr to point to array b */ 10 int i; /* counter */ 11 int offset; /* counter */ 12 13 /* output array b using array subscript notation */ 14 printf( "Array b printed with:\narray subscript notation\n" ); 15 16 /* loop through array b */ 17 for ( i = 0; i < 4; i++ ) { 18 printf( "b[ %d ] = %d\n", i, b[ i ] ); 19 } /* end for */ 20 21 /* output array b using array name and pointer/offset notation */ 22 printf( "\npointer/offset notation where\n" 23 "the pointer is the array name\n" ); 24
Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved. 25 /* loop through array b */ 26 for ( offset = 0; offset < 4; offset++ ) { 27 printf( "*( b + %d ) = %d\n", offset, *( b + offset ) ); 28 } /* end for */ 29 30 /* output array b using bptr and array subscript notation */ 31 printf( "\npointer subscript notation\n" ); 32 33 /* loop through array b */ 34 for ( i = 0; i < 4; i++ ) { 35 printf( "bptr[ %d ] = %d\n", i, bptr[ i ] ); 36 } /* end for */ 37 38 /* output array b using bptr and pointer/offset notation */ 39 printf( "\npointer/offset notation\n" ); 40 41 /* loop through array b */ 42 for ( offset = 0; offset < 4; offset++ ) { 43 printf( "*( bptr + %d ) = %d\n", offset, *( bptr + offset ) ); 44 } /* end for */ 45 46 return 0; /* indicates successful termination */ 47 48 } /* end main */
Array b printed with: Array subscript notation b[ 0 ] = 10 b[ 1 ] = 20 b[ 2 ] = 30 b[ 3 ] = 40 Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Edition Inc. All right Reserved. Pointer/offset notation where the pointer is the array name *( b + 0 ) = 10 *( b + 1 ) = 20 *( b + 2 ) = 30 *( b + 3 ) = 40 Pointer subscript notation bptr[ 0 ] = 10 bptr[ 1 ] = 20 bptr[ 2 ] = 30 bptr[ 3 ] = 40 Pointer/offset notation *( bptr + 0 ) = 10 *( bptr + 1 ) = 20 *( bptr + 2 ) = 30 *( bptr + 3 ) = 40 Program Output
練 列. array++? 列. ptr++?
列 例 int array[3][4], *ptr; ptr=(int *)array; array[1][2] ptr[3*1+2] (*(array+1))[1] *((array+1)[1]) 列 例
列 列裡. 列 int *int_ptr[10]; // 數 列 float *float_ptr[10]; double *double_ptr[10]; char *str[10]; 例 :
數 數 列,, 數, 數 : void (*func)(void); 例 : 數 數 : 9 行 *func(i) func(i),?
(1) int * ptr; /* */ int ** ptr; /* */ int *** ptr; /* 數 */ ptr *ptr **ptr 數
(2)
(3) 9*9 數 列 列 料列 來 列 array[i][j] *(*(array+i)+j)
: 列 行 #include <stdio.h> #include <stdlib.h> int main() { int x[5]={2,4,6,8,10},*p,**pp; p=x; pp=&p; printf("%d\n",*(p)); p++; printf("%d\n",**pp); system("pause"); return 0; }
main() argc argv int main(int argc,char *argv[]) int main(int argc,char argv[][]) /* 列 串 列 */ int main(int argc,char **argv) /* */ argc 令列 參數 串 數. argv 令列 參數 串. 例
列 C 不 列 數 列 行 來 料
malloc( ) #include <stdlib.h> #include <malloc.h> void *malloc(size_t size); 例
free( ) #include <stdlib.h> #include <malloc.h> void free(void *ptr); 例 malloc 不 狀 free
了什 什 列 main 數串列