|
陣列名稱® |
A[0] |
A[1] |
A[2] |
..... |
A[19] |
| 記憶體® |
|
|
|
| |
|
記憶體位址® |
m
|
m+2 |
m+4 |
|
m+2*(19) |
|
陣列名稱® |
score[0] |
score[1] |
score[2] |
score[3] |
score[4] |
| 記憶體® |
2 |
5 |
7 |
9 |
10 |
| Example: #include < stdio.h > void main(void) { int count,total=0; int number[10]={ 1,2,3,4,5,6,7,8,9,10 }; for( count=0 ; count < 10 ; ++count ){printf("number[%d]=%d\n",count,number[count]); total=total + number[count];}printf("1+2+3+4+5+6+7+8+9+10=%d\n",total); } |
執 行 結 果 : number[0]=1number[1]=2number[2]=3number[3]=4number[4]=5number[5]=6number[6]=7number[7]=8number[8]=9number[9]=101+2+3+4+5+6+7+8+9+10=55 |
行 (column) |
|||||
列 |
A[0][0] | A[0][1] | A[0][2] | ...... | A[0][n-1] |
| (row) | A[1][0] | A[1][1] | A[1][2] | ...... | A[1][n-1] |
| A[2][0] | A[2][1] |
A[2][2] | ...... | A[2][n-1] | |
| . |
. |
. |
. |
. | |
| . |
. |
. |
. |
. | |
| A[m-1][0] | A[m-1][1] | A[m-1][2] | A[m-1][n-1] |
| 一 | 二 | 三 | 四 | 五 | |
| 1 | 2 | 2 | |||
| 2 | 1 | 4 | 1 | 4 | 1 |
| 3 | 5 | 5 | 5 | ||
| 4 | |||||
| 5 | 3 | 3 | 3 | ||
| 6 |
| 課程名稱 | 代碼 |
| 計算機概論 | 1 |
| 離散數學 | 2 |
| 資料結構 | 3 |
| 資料庫概論 | 4 |
| 上機實習 | 5 |
|
記憶體位址 |
記憶體 |
|
a (陣列起始位址) |
course [0][0] |
|
a + 2 |
course [0][1] |
|
a + 4 |
course [0][2] |
|
|
|
|
a + 4 |
course [5][3] |
|
a + 4 |
course [5][4] |
|
|
| (0,0) | (0,1) | (0,2) | (0,3) | (0,4) |
| (1,0) | (1,1) | (1,2) | (1,3) | (1,4) |
| (2,0) | (2,1) | (2,2) | (2,3) | (2,4) |
| (3,0) | (3,1) | (3,2) | (3,3) | (3,4) |
| (4,0) | (4,1) | (4,2) | (4,3) | (4,4) |
| (5,0) | (5,1) | (5,2) | (5,3) | (5,4) |
/* ================================================== *//* 程式實例: 查詢上課的課目,其中課程以代碼表示. *//* 0 表示沒課 *//* 1 表示計算機概論 *//* 2 表示離散數學 *//* 3 表示資料結構 *//* 4 表示資料庫概論 *//* 5 表示上機實習; *//* ==================================================*/void main(){ int course[6][5] = { 0, 2, 0, 2, 0, /* 課程定義 */ 1, 4, 1, 4, 1, 5, 0, 5, 0, 5, 0, 0, 0, 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0 }; int week; /* 星期資料變數 */ int class; /* 第幾節課的變數 */ int class_no; /* 課程代碼變數 */ printf("請輸入星期(1 到 5). ==> "); scanf("%d",&week); /* 讀取星期資料 */ printf("請輸入第幾節課(1 到 6). ==> "); scanf("%d",&class); /* 讀取第幾節課 */ class_no = course[class-1][week-1]; /* 課程查詢 */ switch ( class_no ) /* 印出課程名稱 */ { case 0: printf("這節沒課\n"); break; case 1: printf("計算機概論\n"); break; case 2: printf("離散數學\n"); break; case 3: printf("資料結構\n"); break; case 4: printf("資料庫概論\n"); break; case 5: printf("上機實習\n"); break; }} |
| /* ======================================== *//* 計算出一星期的課程總數 *//* ======================================== */ void main(){ int course[6][5] = { 0, 2, 0, 2, 0, /*課程定義 */ 1, 4, 1, 4, 1, 5, 0, 5, 0, 5, 0, 0, 0, 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0 }; int sum; /* 課程總數 */ int i,j; sum = 0; /* 設定課程總數初值 */ for ( i = 0; i < 6; i++ ) /* 二維陣列的走訪 */ for ( j = 0; j < 5; j++ ) if ( course[j] != 0 ) /* 檢查有沒有課 */ sum++; printf("課程總數: %d\n",sum); /* 印出課程總數 */} |
| 行 | ||||||
| 列 | (0,0) | (0,1) | (0,2) | (0,3) | (0,4) | 列一 |
| (1,0) | (1,1) | (1,2) | (1,3) | (1,4) | 列二 | |
| (2,0) | (2,1) | (2,2) | (2,3) | (2,4) | 列三 | |
| (3,0) | (3,1) | (3,2) | (3,3) | (3,4) | 列四 | |
| (4,0) | (4,1) | (4,2) | (4,3) | (4,4) | 列五 | |
| (5,0) | (5,1) | (5,2) | (5,3) | (5,4) | 列六 | |
| 行一 | 行二 | 行三 | 行四 | 行五 | ||
| 列一 | 列二 | 列六 | ||||||||||||||
| (0,0) | (0,1) | (0,2) | (0,3) | (0,4) | (1,0) | (1,1) | (1,2) | (1,3) | (1,4) | … | (5,0) | (5,1) | (5,2) | (5,3) | (5,4) | |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 25 | 26 | 27 | 28 | 29 | ||
| 行一 | 行二 | 行六 | |||||||||||||||||
| (0,0) | (1,0) | (2,0) | (3,0) | (4,0) | (5,0) | (0,1) | (1,1) | (2,1) | (3,1) | (4,1) | (5,1) | … | (0,4) | (1,4) | (2,4) | (3,4) | (4,4) | (5,4) | |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | ||
1.
以列為主:i*c+j
2.
以行為主:j*r+i
| Example: #include < stdio.h > void main(void){ char string1[5]={'A','B','C','D','E'}; char string2[6]={'A','B','C','D','E','\0'};char string3[6]="ABCDE\0";char string4[5]="ABCDE"; printf("string1 = \"%s\"\n",string1);printf("string2 = \"%s\"\n",string2);printf("string3 = \"%s\"\n",string3);printf("string4 = \"%s\"\n",string4);} |
執 行 結 果 : string1="ABCDE?ABCDE" string2 = "ABCDE" string3 = "ABCDE" string4 = "ABCDE" |
|
ptr為 指 標 變 數 |
|
||
| ® 變數ptr ® |
1010 H | ||
| 0800H | |||
|
0802H |
|||
|
….. |
… | ||
|
1010H |
16 | ¬*ptr | |
|
|
記憶體 |
| Example: #include < stdio.h > void main(void){int x=10;int *y;y=&x;printf(" x = %d\n", x);printf("&x = %x\n",&x);printf("*y = %d\n",*y);printf(" y = %x\n", y);} |
執行結果 : x = 10 &x = ffda *y = 10 y = ffda |
| Example: #include < stdio.h > change(x,y) int *x,*y; {int temp; temp=*x; *x=*y;*y=temp; } void main(void){ int m=3,n=4;printf(" m = %d n= %d \n\n",m,n);printf(" m = %d n= %d \n\n",m,n); change(&m,&n); printf(" m = %d n= %d \n",m,n); } |
執行結果 : m = 3 n= 4m = 4 n= 3 |
l
For example:
int array[5]={ 1,2,3,4,5 };
| 陣列位址 | 指標相對位址 | |
| array[0] | 1 | *(array+0) |
| array[1] | 2 | *(array+1) |
| array[2] | 3 | *(array+2) |
| array[3] | 4 | *(array+3) |
| array[4] | 5 | *(array+4) |
| Example: #include < stdio.h > void main(void) { int array[5]={ 1,2,3,4,5 }; int count; for ( count=0 ; count<5 ; ++count ) printf("array[%d]=%d<-->*(array+%d\)=%d\n",count,array[count], count,*(array+count)); } |
執行結果 : array[0]=1 <--> *(array+0)=1array[1]=2 <--> *(array+1)=2array[2]=3 <--> *(array+2)=3array[3]=4 <--> *(array+3)=4array[4]=5 <--> *(array+4)=5 |
| Example: #include < stdio.h > void main(void) { char string[7]="POINTER"; int count; for (count=0 ; count<7 ; ++count ) printf("string[%d]=%c <--> *\(string+%d\)=%c\n", count, string[count], count,*(string+count)); } |
執行結果 : string[0]=P <--> *(string+0)=Pstring[1]=O <--> *(string+1)=Ostring[2]=I <--> *(string+2)=Istring[3]=N <--> *(string+3)=Nstring[4]=T <--> *(string+4)=Tstring[5]=E <--> *(string+5)=Estring[6]=R <--> *(string+6)=R |
l
| 假設指標ptr所指資料為整數,且其為指為1666 | 指令 | 執行結果 | 執行動作 |
| ptr++ | 1668 | 指向下一個整數位址 | |
| ptr-- | 1664 | 指向前一個整數位址 | |
| ptr+7 | 1676 | 指向第七個整數位址 |
| 歡迎光臨 中央論壇 - CENTER BBS (https://centerbbs.com/) | Powered by Discuz! X3 |