C語言練習題:指標(C language exercise: Pointer)
指標的慣念可以看 C語言: 超好懂的指標,初學者請進 Pointer concepts: 1. Pointers in C Programming: What is Pointer, Types & Examples 2. Introduction to C Pointers 🧩 學習脈絡 基礎操作 :從宣告、取值開始(練習一~三)。 進階應用 :陣列、指標運算、字串處理(練習四~八)。 安全與抽象 :空指標檢查、函式指標(練習九~十)。 ⚠️ 注意事項 記憶體管理 : malloc() 與 free() 必須成對使用,避免記憶體洩漏。 空指標檢查 :存取 NULL 指標會造成程式崩潰。 函式指標 :靈活但容易出錯,需確保型別一致。 練習一:基本語法 宣告指標、取變數位址、透過指標取值。 範例程式展示 & 與 * 的基本用法。 設計一個C語言程式來呈現指標的語法,例如宣告、取址、取值等。 Exercise 1: Basic Syntax Design a C program to demonstrate the basic syntax of pointer. Such as declaration, address and value. 練習一參考解法: Exercise 1 solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /* Pointer Basic Syntax Author: Holan */ #include <stdio.h> #include <stdlib.h> int main () { int n = 50 ; // declaration int * ip; // assignment ip = & n; printf( "The value of &n:%X \n " , & n); printf( "The value of n:%i \n " , n); printf( "T...