C++ 程式設計

Similar documents
untitled

I/O Files讀寫檔案:

微處理機實習期末專題

移民資料

untitled

untitled

了 立 連 立 量 領 來 例 蘭 便 不 數 不 論 更 更 更 力 更 參 例 來 例 見 量 度 量 量 參 論 量 行 量 量 瑩 理 來 錄 量 量 不 力 省 力 立 力 量 量 量 了 量 便 錄 錄 錄 料 說 省 6

Fuzzy GP

untitled

untitled

ARBURG Qualitätssicherung AQS 4.0

PowerPoint Presentation

c_cpp

94年度學習障礙補救教學進階研習

untitled

untitled

untitled

第五章 鄉鎮圖書館閱讀推廣活動之分析

untitled

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

untitled

untitled

自學進修學力鑑定考試職業證照與專科學校類科及筆試科目對照表

untitled

untitled

untitled

untitled

untitled

健康與食品安全的問卷訪問

逢 甲 大 學

台灣經濟新報資料庫

吃寒天真的能減肥嗎

untitled

九十三年第三期檔案管理工作研習營學員建議事項答覆情形彙整表

中華民國第45屆中小學科學展覽會

untitled

untitled

嘉義市都市計畫保護區農業區土地使用審查要點(草案)

untitled

臺灣地區的警察教育現況與展望

Powerpoint 2003

untitled

第一章 簡介

個人教室 / 網路硬碟

untitled

untitled

投影片 1

十四、特殊需求的嬰兒

STANDARD

untitled

四湖鄉志纂修

「UPENN牙醫學院見習及費城文藝之旅」成果報告書

1

專 題 論 述

untitled

untitled

第3章.doc

untitled

untitled

公告99年度全民健康保險醫療給付費用總額及其分配

隱形眼鏡的世界

untitled

國立陽明大學輻射防護計畫書

untitled

untitled

untitled

untitled

第一章 導論

untitled

費協會第152次會議

地方公共服務績效比較評量之探討—標竿學習策略的觀點

第五章 實例個案

大陸黨報集團化發展之研究

untitled

人身保險業務員資格測驗方案

PowerPoint 簡報

untitled

untitled

untitled

untitled

untitled

電腦組裝訓練

untitled

untitled

壹、

untitled

untitled

untitled

untitled

untitled

untitled

untitled

untitled

中國大陸女排成功之道

untitled

untitled

untitled

untitled

untitled

untitled

Transcription:

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 數串列