Re: [心得] 晶睿面試考題

看板Soft_Job作者 (赤木巧☠)時間11年前 (2014/10/09 13:19), 11年前編輯推噓1(103)
留言4則, 3人參與, 最新討論串1/1
※ 引述《lovesnake (LoyalDog)》之銘言: : 4. 死在這一題...死不瞑目要把完整題PO上來。 : Run the follow code on 32bit little-endian PC. What is the final result? : char hello[] = "hello"; : int data[] = {348, 152, 65538, NULL, 32, 222}; : void *p1 = data - 1; : short *p2 = (short *)p1 + sizeof(hello); : int *p3 = (int *)((char *)p2 + (char)*p2); : printf("%d", *p3); 看到這題忽然想閒聊一下。 很顯然他是想考byte排列順序跟指標操作的題目。 不過我比較有疑問的一點就是: 我依稀記得,C/C++的標準下,沒有規範數值類型實際的byte數,只規定最小要幾個byte。 (只有char固定是1,以及int32_t這類指定長度的數值) 姑且就當int在32-bit系統是4 bytes好了。 好像沒有規定32-bit系統下,short一定要2 bytes不能是4 bytes。 只有sizeof(short) <= sizeof(int) <= sizeof(long)這個原則。 所以這樣看來short *p2 = (short *)p1 + sizeof(hello);這答案是啥似乎就很難說了。 如果我搞錯了什麼請鞭小力一點,只是忽然想到閒聊一下XD -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.131.77 ※ 文章網址: http://www.ptt.cc/bbs/Soft_Job/M.1412831940.A.996.html ※ 編輯: xtxml (125.227.131.77), 10/09/2014 13:23:56

10/10 01:45, , 1F
10/10 01:45, 1F

10/11 02:02, , 2F
題目中hello不是個array嗎? sizeof結果是 5吧?
10/11 02:02, 2F

10/13 12:12, , 3F
sizeof不會錯,但p2的實際位置在哪就不一定了
10/13 12:12, 3F

10/14 09:50, , 4F
附帶一提,"hello"的sizeof應該是6
10/14 09:50, 4F
文章代碼(AID): #1KDXh4cM (Soft_Job)