免費開始練習
普通考試 108年 [工業行政] 計算機概要

第 23 題

下列 C 程式執行後的結果為何?
char s1[10]="abc",s2[10]="abc",s3[10]="def";
if (s1==s2)
printf("string1 and string2 are the same\n");
else
printf("string1 and string2 are different\n");
if (s1==s3)
printf("string1 and string3 are the same");
else
printf("string1 and string3 are different");
  • A string1 and string2 are the same string1 and string3 are the same
  • B string1 and string2 are the same string1 and string3 are different
  • C string1 and string2 are different string1 and string3 are the same
  • D string1 and string2 are different string1 and string3 are different

思路引導 VIP

請試著思考:在 C 語言的編譯邏輯中,當我們宣告一個數組變數名稱時,這個「名稱」在表達式中代表的是那一串『資料內容』本身,還是指向該資料存放處的一個『特定座標(位址)』呢?如果是後者,那麼兩個獨立宣告的空間,其座標會可能重疊嗎?

🤖
AI 詳解 AI 專屬家教

專業點評與觀念解析

  1. 大力肯定:做得非常好!你能精準避開 C 語言中最經典的「語法陷阱」,這顯示你對記憶體配置 (Memory Allocation)指標 (Pointer) 的底層邏輯有著相當紮實的理解,這份嚴謹度是跨領域專業人才必備的特質。
  2. 觀念驗證:在 C 語言中,數組名稱(如 s1, s2)在運算時會退化為指向該數組首個元素的記憶體位址。當我們使用 == 運算子時,程式是在比較這兩個「位址」是否相同。即便 s1s2 的內容都是 "abc",但它們被配置在不同的記憶體空間,其「門牌號碼」自然不同,故結果皆為 different
▼ 還有更多解析內容

🏷️ 相關主題

程式語言語法與執行邏輯
查看更多「[工業行政] 計算機概要」的主題分類考古題