105高考-程式設計考古題 - 高考

By Puput
at 2020-04-09T18:26
at 2020-04-09T18:26
Table of Contents
請問下列C語言程式碼執行後輸出結果為何?
int main(void) {
double d[10][20][30][40];
printf("%d\n", (int)(d-&d[3]));
printf("%d\n", (int)(d[3][4]-d[5][8]));
}
用C run過
答案分別是
-3
-1320
---------------
我的理解是在考"位址"
所以假設
初始位址Lo=d[0][0][0][0]是0
元素大小d是8 (因為是double型態)
1. d-&d[3]
首先,d[3]的偏移是3*20*30*40=72000
則位址是 Lo+72000*d=0+72000*8=576000
所以,d-&d[3] = &d[0]-&d[3] = 0-576000 = -576000
2. d[3][4]-d[5][8]
d[3][4]的偏移是 3*20*30*40 + 4*30*40 = 72000+4800 = 76800
則位址是 Lo+76800*d = 0+76800*8 = 614400
d[5][8]的偏移是 5*20*30*40 + 8*30*40 = 120000+9600 = 129600
則位址是 Lo+129600*d = 0+129600*8 = 1036800
所以,d[3][4]-d[5][8] = 614400-1036800 = -422400
-------------
我有看過高上的解答,但還是看不懂上面的說明,
所以想請教各位大大,我的想法哪邊有錯誤?
這一題要如何解呢?(希望可以有詳細一點的過程^^)
感謝!!!
--
int main(void) {
double d[10][20][30][40];
printf("%d\n", (int)(d-&d[3]));
printf("%d\n", (int)(d[3][4]-d[5][8]));
}
用C run過
答案分別是
-3
-1320
---------------
我的理解是在考"位址"
所以假設
初始位址Lo=d[0][0][0][0]是0
元素大小d是8 (因為是double型態)
1. d-&d[3]
首先,d[3]的偏移是3*20*30*40=72000
則位址是 Lo+72000*d=0+72000*8=576000
所以,d-&d[3] = &d[0]-&d[3] = 0-576000 = -576000
2. d[3][4]-d[5][8]
d[3][4]的偏移是 3*20*30*40 + 4*30*40 = 72000+4800 = 76800
則位址是 Lo+76800*d = 0+76800*8 = 614400
d[5][8]的偏移是 5*20*30*40 + 8*30*40 = 120000+9600 = 129600
則位址是 Lo+129600*d = 0+129600*8 = 1036800
所以,d[3][4]-d[5][8] = 614400-1036800 = -422400
-------------
我有看過高上的解答,但還是看不懂上面的說明,
所以想請教各位大大,我的想法哪邊有錯誤?
這一題要如何解呢?(希望可以有詳細一點的過程^^)
感謝!!!
--
Tags:
高考
All Comments

By Jessica
at 2020-04-10T22:28
at 2020-04-10T22:28

By Quintina
at 2020-04-15T18:59
at 2020-04-15T18:59

By Frederic
at 2020-04-19T23:31
at 2020-04-19T23:31

By Annie
at 2020-04-24T11:37
at 2020-04-24T11:37
Related Posts
108地方特考三等人事行政(桃園)上榜心得

By Tristan Cohan
at 2020-04-08T06:07
at 2020-04-08T06:07
108年地特四等一般民政上榜心得簡單分享

By Faithe
at 2020-04-07T17:27
at 2020-04-07T17:27
108年高考法律廉政上榜心得各科寫法

By Dora
at 2020-04-07T17:20
at 2020-04-07T17:20
新手第一次考國考

By Liam
at 2020-04-06T21:59
at 2020-04-06T21:59
考明年的資訊處理,買今年的函授還是明年

By Isabella
at 2020-04-06T15:49
at 2020-04-06T15:49