發表文章

Mastering Pointers in C/C++: 10 Practical Coding Exercises for Beginners

If you are diving into C or C++ programming, there is one concept you simply cannot escape: Pointers . Often considered the first major hurdle for beginners, pointers are actually incredibly powerful tools that give you direct access to memory and help you write highly efficient code. Before jumping into the exercises below, if you need a quick conceptual refresher, I highly recommend checking out some classic guides such as [Pointers in C Programming: What is Pointer, Types & Examples] or an [Introduction to C Pointers]. To help you solidifying your understanding, here are 10 practical pointer exercises , complete with reference solutions. Let's code! Exercise 1: Basic Pointer Syntax Goal: Design a C program to demonstrate the foundational syntax of pointers, including pointer declaration, address-of retrieval ( & ), and dereferencing ( * ). Reference Solution: /* Pointer Basic Syntax Author: Holan */ # include <stdio.h> # include <stdlib.h> int main () ...

掌握樹莓派的瑞士刀:Raspbian / Debian Linux 常用指令(Commonly Used Raspberry Pi Commands)

對於剛接觸樹莓派(Raspberry Pi)的玩家來說,從圖形介面走向「終端機(Terminal)」是核心的必經之路。Raspbian 作業系統基於 Debian Linux,因此這裡整理的指令,不僅在樹莓派上暢行無無阻,在多數的 Linux 環境中也完全通用。 本文根據筆者多年的實戰經驗,將常用的指令分為 檔案系統 、 搜尋 、 網路 以及 系統進階 四大類,方便大家隨時查閱。 一、 檔案系統與路徑管理 這是最頻繁使用的基本動作,用來在樹莓派的資料夾世界中穿梭與操控檔案。 1. 瀏覽與路徑切換 ls (List files) :列出目前路徑下的檔案與資料夾。 常用變化 : ls -al 可以連同隱藏檔、權限、檔案大小一起詳細列出。 cd (Change Directory) :切換資料夾。 常用變化 : cd .. 回到上一層; cd ~ 回到使用者的家目錄。 pwd (Print Working Directory) :顯示目前所在的工作資料夾完整路徑。 2. 資料夾與檔案操作 mkdir (Make Directory) :建立新資料夾。 範例 : mkdir project rmdir (Remove Directory) :刪除「空」的資料夾。 rm (Remove) :刪除檔案。 常用變化 : rm -rf folder_name 強制刪除整個資料夾及其所有內容(使用時請務必小心)。 cp (Copy) :複製檔案或資料夾。 範例 : cp file1.txt file2.txt (複製資料夾需加 -r )。 mv (Move) :移動檔案或資料夾,同時也常用來做「重新命名」。 範例 : mv old.txt new.txt (重新命名)。 touch :若檔案不存在則建立一個新的空檔案;若檔案已存在則更新其最後修改時間。 範例 : touch script.py 3. 讀取檔案內容 cat (Concatenate) :在終端機直接印出檔案的完整內容。 head :只顯示檔案的前幾行(預設為前 10 行)。 tail :只顯示檔案的最後幾行。 常用變化 : tail -f log.txt 可以即時監控檔案的更新,適合看 log。 4. 權限與擁有者管理 chmod (Change Mode) :修改檔案或資料夾的權限。 範...

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...