# 2018q3 Homework1 contributed by <`kdy136811`> :::info 請及早安排 **連續** 的時間來達成作業要求 :notes: jserv ::: ## 你所不知道的C語言 ### 開發工具和規格標準 - `int main()`和`void main()`的區別 在 [ISO/IEC 9899 (a.k.a C99 Standard)](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf) 中 5.1.2.2.1 中的Program startup中提到: :::info It shall be defined with a **return type of int and with no parameters** ```clike int main(void) { /* ... */}; ``` or with **two parameters (referred to here as argc and argv)** ```Clike int main(int argc, char *argv[]) { /* ... */ }; ``` ::: 因此`void main()`是**錯誤的**寫法。 ### ISO/IEC 9899(C99) - `&`的正確念法應該是"address of" - Array, function, and pointer types are collectively called derived declarator types. 由此可知陣列、函式、指標都是**衍生的宣告型態**。 - Arithmetic types and pointer types are collectively called **scalar types(純量)**. Array and structure types are collectively called aggregate types. 當我們在程式碼中看到`++、--、+=、-=`等等操作時,都是對**純量**進行。而另外在pointer type中嘗試以下程式碼: ```C=1 #include "stdio.h" int main() { int *ptrA, a; ptrA = &a; int *ptrB = ptrA+1; printf("%d\n%d", ptrA, ptrB); } ``` - 執行結果: :::success 6487612 6487616 ::: - 由此可知對一pointer type的變數進行加減時,是加減一個**單位**而非純量的1。 ### 指標篇 #### function pointer - 如同各種資料型態或陣列,函式在記憶體中也佔有空間,而函式的名稱即為指向該記憶體空間的名稱。因此當呼叫函式時,就會透過函式名稱找到對應的空間,執行其中的程式碼。 **因此我們可以透過function pointer指向某個函式,找到它的記憶體空間並執行相同的程式碼。** 以下做一個簡單的程式練習: ```C=1 #include "stdio.h" int test(); int main() { int (*funcptr)(); funcptr = test; test(); funcptr(); } int test() { printf("hello\n"); } ``` - 執行結果: :::success hello hello ::: #### void *之謎