引用 | 編輯
GNUGCC
2007-06-17 13:55 |
3樓
▲ ▼ |
指標在處理陣列時要非常的小心, 在複雜度上也有一些挑戰性...
下面是一個範例 : for ( int i = 0; i < 3; i++ ) { for ( int j = 0; j < 5; j++ ) cout << *(*(array + i) + j); cout << endl; } x0 |
引用 | 編輯
karen7710
2007-06-17 21:22 |
4樓
▲ ▼ |
以下是在網路上找到的程式,要如何改成.c檔可以用的程式?
複製程式 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15}}; int *ptr=array[0]; for(int i=0;i<3;i++) for(int j=0;j<5;j++) cout<<"array["<<i<<"]["<<j<<"]= "<<*(ptr+i*5+j)<<endl; system("PAUSE"); return EXIT_SUCCESS; } x0 |
引用 | 編輯
gamewalk
2007-06-17 22:58 |
5樓
▲ ▼ |
.c檔的話...不要用using namspace 還有include都要改掉 若要列印的話改成用printf , 還有把for裡面的int i , int j 放在外面宣告試試....
x0 |
引用 | 編輯
karen7710
2007-06-18 08:37 |
6樓
▲ ▼ |
以下的程式都無法跑東西出來!
複製程式 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15}}; int *ptr=array[0]; int i,j; for(i=0;i<3;i++) for(j=0;j<5;j++) printf("%s",array[i][j]); system("pause"); return 0; } x0 |
引用 | 編輯
gamewalk
2007-06-18 08:55 |
7樓
▲ ▼ |
你要列印的應該是數字吧...為什麼你用%s , 還有既然是說了要用指位器array[ i ][ j ] 這邊應該改成GNUGCC大大的方法 *(*(array + i) + j) 或者你自己找的 *(ptr+i*5+j) 這樣比較符合題目吧....
x0 |
引用 | 編輯
karen7710
2007-06-18 10:34 |
8樓
▲ ▼ |
可以執行出來了,可是最後會多印一個0,要怎麼解決呢?
複製程式 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15}}; int *ptr=array[0]; int i,j; for(i=0;i<3;i++) for(j=0;j<5;j++) printf(" %d", *(*(array + i) + j)); system("pause"); return 0; } x0 |
引用 | 編輯
GNUGCC
2007-06-18 13:20 |
9樓
▲ ▼ |
int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15}};
在第 3 個只有放 4 個變數所以最後一個會被設為 0... 妳只要在那裡加一個數字就可以了...^^ x0 |
引用 | 編輯
karen7710
2007-06-18 16:03 |
11樓
▲ ▼ |
我多用一個數字,最後在用\b把它刪掉,這樣可以吧?
複製程式 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int array[3][5]={{2,3,4,5,6},{7,8,9,10,11},{12,13,14,15,1}}; int *ptr=array[0]; int i,j; for(i=0;i<3;i++) for(j=0;j<5;j++) printf(" %d", *(*(array + i) + j)); printf("\b"); system("pause"); return 0; } x0 |
引用 | 編輯
GNUGCC
2007-06-18 16:16 |
12樓
▲ ▼ |
即然題目這樣的話那就用另外一種方法...
妳可以判斷是否 > 0 再做輸出 : if ( *(*(array + i) + j ) > 0 ) cout << *(*(array + i) + j); 不過照常理來看的話宣告一個二維陣列通常裡面會放入剛好的數值在裡面... 假如不夠的話系統會自動設為 0 所以會印出 0... x1 |