--- tags: C --- C語言的變數的宣告、定義 (含指標與自訂型別) === + [執行程式](https://repl.it/join/jvusmyza-aidanlu) + Forward Declaration[~[1]~](https://en.wikipedia.org/wiki/Forward_declaration),[~[2]~](https://zh.wikipedia.org/wiki/%E5%89%8D%E5%90%91%E5%A3%B0%E6%98%8E) + 延伸學習:[C語言的指標範例](https://hackmd.io/@6djlj9vRR1GZP5OhFxxMWA/H1ZY_i35w) ___ ### Case#1 ```c= #include <stdio.h> #include <stdlib.h> int main() { int a; printf("a = %d\n", a); } ``` :::spoiler 結果 :::success > a = 0[color=green] ::: ### Case#2 ```c= #include <stdio.h> #include <stdlib.h> int main() { int* a; printf("*a = %d\n", *a); } ``` :::spoiler 結果 :::danger 可通過編譯,但是<font color="#00f">**執行時會錯誤</font>** ::: ### Case#3 ```c= #include <stdio.h> #include <stdlib.h> int main() { int* a = malloc(sizeof(int)); *a = 10; printf("*a = %d\n", *a); } ``` :::spoiler 結果 :::success > *a = 10[color=green] ::: ### Case#4 ```c= #include <stdio.h> #include <stdlib.h> struct Point; //Forward Declaration int main() { struct Point p; } ``` :::spoiler 結果 :::danger 無法通過編譯 ::: ### Case#5 ```c= #include <stdio.h> #include <stdlib.h> struct Point; //Forward Declaration struct Point { int x, y; }; int main() { struct Point p; p.x = 10; printf("p.x = %d\n", p.x); } ``` :::spoiler 結果 :::success > p.x = 10[color=green] ::: ### Case#6 ```c= #include <stdio.h> #include <stdlib.h> struct Point; //Forward Declaration struct Point { int x, y; }; int main() { struct Point* p; p->x = 10; printf("p->x = %d\n", p->x); } ``` :::spoiler 結果 :::danger 可通過編譯,但是<font color="#00f">**執行時會錯誤</font>** ::: ### Case#7 ```c= #include <stdio.h> #include <stdlib.h> struct Point; struct Point { int x, y; }; int main() { struct Point* p = malloc(sizeof(struct Point)); p->x = 10; printf("p->x = %d\n", p->x); } ``` :::spoiler 結果 :::success > p->x = 10[color=green] ::: ## 延伸學習:[C語言的指標範例](https://hackmd.io/@6djlj9vRR1GZP5OhFxxMWA/H1ZY_i35w)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up