Skip to content

C-1.3 Arrays & Strings

C ไม่มี string type — มันมีแค่ array ของ char ที่จบด้วย \0 ฟังดูดิบ แต่นี่คือสิ่งที่ซ่อนอยู่ใต้ทุก string ในทุกเครื่องมือ

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: 85
Second: 92
Last: 95
Total size: 16 bytes
Each int: 4 bytes
Count: 4

สังเกต: sizeof(scores) = 16 เพราะ 4 ints x 4 bytes = 16 bytes — C บอกคุณตรงๆ ว่า array ใช้ memory เท่าไหร่

นี่คือ 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: HELLO
sizeof: 6 bytes
strlen: 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

#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 bytes
strlen: 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,\0
SQL: CHAR(10) = 'HI' → 'H','I',' ',' ',' ',' ',' ',' ',' ',' '

ต่างกันแค่: C เติม \0 แต่ SQL เติม spaces