2013-03-29 79 views
0

如果調用等的函數:ANSI C - 傳遞值陣列

coworkers = engineers(1,0,3); 
... 

int engineers(unsigned char nerd, unsigned char dweeb[], unsigned char derp) { 
    printf("Is this the address of dweeb[0]? 0x%x \n", dweeb); 
    printf("Is this the value of dweeb[0]? %x \n", dweeb[0]); 
} 

Output: 
Is this the address of dweeb[0]? 0xfeedbeef 
Is this the value of dweeb[0]? 

當我通過0值呆子[]這是否成爲呆子[0]的值?

- 我是個大笑,週五快樂。謝謝

回答

0

dweeb[0]相當於*(dweeb+0)所以是的,它確實成爲值在dweeb[0]

注意,因爲呆子+ 0 = 0 +呆子,你也可以寫爲0[dweeb]


在仔細一看,當你通過0engineers功能作爲第二個參數,什麼情況是,該地址0x0將用作陣列dweeb的位置。取消引用該地址(如dweeb[0])是未定義的行爲。最可能的結果是程序崩潰。你會怎樣實際上看到的是更容易:

Is this the address of dweeb[0]? 0x0 
Segmentation fault 

我不知道你在問什麼,如果它是:「如果我把括號0dweep[ ]會發生什麼」或「如果我通過會發生什麼0作爲值爲dweep