C-1.3 Arrays & Strings
C ไม่มี string type — มันมีแค่ array ของ char ที่จบด้วย
\0ฟังดูดิบ แต่นี่คือสิ่งที่ซ่อนอยู่ใต้ทุก string ในทุกเครื่องมือ
Arrays ใน C — พื้นฐาน
Section titled “Arrays ใน C — พื้นฐาน”Array คือ กล่องเรียงต่อกัน ในหน่วยความจำ แต่ละกล่องมีขนาดเท่ากัน:
int scores[4] = {85, 92, 78, 95};
Memory layout:┌────┬────┬────┬────┐│ 85 │ 92 │ 78 │ 95 │└────┴────┴────┴────┘ [0] [1] [2] [3] ← index เริ่มที่ 0#include <stdio.h>
int main() { int scores[4] = {85, 92, 78, 95};
// เข้าถึงด้วย index (เริ่มที่ 0) printf("First: %d\n", scores[0]); // 85 printf("Second: %d\n", scores[1]); // 92 printf("Last: %d\n", scores[3]); // 95
// ขนาดทั้ง array printf("\nTotal size: %zu bytes\n", sizeof(scores)); // 16 printf("Each int: %zu bytes\n", sizeof(scores[0])); // 4 printf("Count: %zu\n", sizeof(scores) / sizeof(scores[0])); // 4
return 0;}ผลลัพธ์:
First: 85Second: 92Last: 95
Total size: 16 bytesEach int: 4 bytesCount: 4สังเกต: sizeof(scores) = 16 เพราะ 4 ints x 4 bytes = 16 bytes — C บอกคุณตรงๆ ว่า array ใช้ memory เท่าไหร่
Array ใน Sheets = ช่วงเซลล์ เช่น A1:A4 — index ไม่ได้เริ่มที่ 0 แต่เริ่มที่ 1
=INDEX(A1:A4, 1) → ค่าแรก (Sheets เริ่มที่ 1 ไม่ใช่ 0)scores = [85, 92, 78, 95]scores[0] # 85 — เริ่มที่ 0 เหมือน CPython ยืม zero-based indexing มาจาก C โดยตรง
-- SQL ไม่มี arrays แบบ C แต่ PostgreSQL มี:SELECT (ARRAY[85, 92, 78, 95])[1]; -- 85 (SQL เริ่มที่ 1!)char Arrays = Strings
Section titled “char Arrays = Strings”นี่คือ insight หลัก: C ไม่มี string type — string คือ char array ที่จบด้วย \0 (null terminator)
#include <stdio.h>#include <string.h>
int main() { // ทั้ง 2 บรรทัดนี้เหมือนกัน: char word[] = "HELLO"; // char word[] = {'H', 'E', 'L', 'L', 'O', '\0'};
printf("String: %s\n", word); printf("sizeof: %zu bytes\n", sizeof(word)); // 6 ไม่ใช่ 5! printf("strlen: %zu chars\n", strlen(word)); // 5
// ดูทีละตัว printf("\n--- ดูทีละ byte ---\n"); for (int i = 0; i < (int)sizeof(word); i++) { printf("word[%d] = '%c' (ASCII %d)\n", i, word[i], word[i]); }
return 0;}ผลลัพธ์:
String: HELLOsizeof: 6 bytesstrlen: 5 chars
--- ดูทีละ byte ---word[0] = 'H' (ASCII 72)word[1] = 'E' (ASCII 69)word[2] = 'L' (ASCII 76)word[3] = 'L' (ASCII 76)word[4] = 'O' (ASCII 79)word[5] = '' (ASCII 0) ← null terminator!ทำไม sizeof = 6 แต่ strlen = 5?
Memory layout ของ "HELLO":┌───┬───┬───┬───┬───┬────┐│ H │ E │ L │ L │ O │ \0 │└───┴───┴───┴───┴───┴────┘ 72 69 76 76 79 0 ← ASCII values [0] [1] [2] [3] [4] [5]
sizeof = 6 bytes (รวม \0)strlen = 5 chars (ไม่นับ \0)\0 คือ null terminator — มันบอก C ว่า “string จบตรงนี้” ถ้าไม่มี \0 C จะอ่านไปเรื่อยๆ ใน memory จนเจอ 0 หรือ crash
Sheets ซ่อน null terminator — คุณพิมพ์ “HELLO” ได้ 5 ตัว =LEN("HELLO") = 5
แต่เบื้องหลัง Sheets เก็บ terminator ไว้ คุณแค่ไม่เห็นมัน
word = "HELLO"len(word) # 5 — Python ไม่นับ \0 เหมือน strlen()Python ซ่อน null terminator ไว้ใน C implementation ข้างใต้
SELECT LENGTH('HELLO'); -- 5 (ไม่นับ terminator)SELECT 'HELLO'::CHAR(10); -- 'HELLO ' (เติม spaces)Fixed-Size char Arrays = SQL CHAR(n)
Section titled “Fixed-Size char Arrays = SQL CHAR(n)”#include <stdio.h>#include <string.h>
int main() { // Fixed-size: จอง 10 bytes เสมอ char fixed[10] = "HI";
printf("Content: '%s'\n", fixed); printf("sizeof: %zu bytes\n", sizeof(fixed)); // 10 printf("strlen: %zu chars\n", strlen(fixed)); // 2
// ดูว่า bytes ที่เหลือเป็นอะไร printf("\n--- ดูทุก byte ---\n"); for (int i = 0; i < 10; i++) { if (fixed[i] == '\0') { printf("fixed[%d] = \\0 (null, ASCII 0)\n", i); } else { printf("fixed[%d] = '%c' (ASCII %d)\n", i, fixed[i], fixed[i]); } }
return 0;}ผลลัพธ์:
Content: 'HI'sizeof: 10 bytesstrlen: 2 chars
--- ดูทุก byte ---fixed[0] = 'H' (ASCII 72)fixed[1] = 'I' (ASCII 73)fixed[2] = \0 (null, ASCII 0)fixed[3] = \0 (null, ASCII 0)fixed[4] = \0 (null, ASCII 0)fixed[5] = \0 (null, ASCII 0)fixed[6] = \0 (null, ASCII 0)fixed[7] = \0 (null, ASCII 0)fixed[8] = \0 (null, ASCII 0)fixed[9] = \0 (null, ASCII 0)char fixed[10] จอง 10 bytes เสมอ ไม่ว่าจะเก็บข้อความสั้นแค่ไหน — ที่เหลือเป็น \0
นี่คือที่มาโดยตรงของ SQL CHAR(10):
C: char fixed[10] = "HI" → 'H','I',\0,\0,\0,\0,\0,\0,\0,\0SQL: CHAR(10) = 'HI' → 'H','I',' ',' ',' ',' ',' ',' ',' ',' 'ต่างกันแค่: C เติม \0 แต่ SQL เติม spaces
Sheets ไม่มี fixed-size strings — ทุกเซลล์ยืดหดตามข้อมูล เหมือน VARCHAR ไม่ใช่ CHAR
# Python strings ยืดหดอัตโนมัติ — ไม่มี fixed-sizeword = "HI" # ใช้แค่ 2 chars + overheadCREATE TABLE demo ( fixed_col CHAR(10), -- จอง 10 bytes เสมอ = C char[10] flex_col VARCHAR(10) -- ใช้เท่าที่ต้องการ (max 10));INSERT INTO demo VALUES ('HI', 'HI');SELECT LENGTH(fixed_col), LENGTH(flex_col) FROM demo;-- 10, 2