Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

ความแตกต่างระหว่าง strlen() และ sizeof() สำหรับ String ใน C

ทั้งสองฟังก์ชันใช้หาขนาด/ความยาว แต่วัดสิ่งที่ต่างกันและมี Semantics ที่ต่างกันอย่างสิ้นเชิง

strlen() - String Length

นับจำนวนตัวอักษรใน String จนกว่าจะเจอ Null Terminator (\0) ไม่รวม Null Terminator เอง

ไวยากรณ์

#include <string.h>
size_t strlen(const char *str);

ตัวอย่าง

#include <stdio.h>
#include <string.h>

int main() {
    char s1[10] = "Hello";  // Array ขนาด 10, String "Hello" (5 chars + \0)
    int len = strlen(s1);
    printf("Length of string s1: %d\n", len);  // 5
    
    char s2[] = "Hello";    // Array ขนาด 6 (Auto-sized)
    printf("Length of s2: %zu\n", strlen(s2)); // 5
    
    char s3[100] = "Hi";    // Array ขนาด 100, String "Hi"
    printf("Length of s3: %zu\n", strlen(s3)); // 2
    
    return 0;
}

ผลลัพธ์

Length of string s1: 5
Length of s2: 5
Length of s3: 2

sizeof() - Object Size in Bytes

คืนขนาดในหน่วย Bytes ของ Object/Type/Variable ที่ Compile Time รวม Null Terminator ด้วย (ถ้าเป็น Array ที่ Init ด้วย String Literal)

ไวยากรณ์

sizeof(type)
sizeof expression

ตัวอย่าง

#include <stdio.h>

int main() {
    int a = 16;
    char s1[10] = "Hello";  // Array[10]
    char s2[] = "Hello";    // Array[6]
    char *s3 = "Hello";     // Pointer to String Literal
    
    printf("Size of variable a (int): %zu\n", sizeof(a));        // 4
    printf("Size of int: %zu\n", sizeof(int));                    // 4
    printf("Size of char: %zu\n", sizeof(char));                  // 1
    printf("Size of float: %zu\n", sizeof(float));                // 4
    printf("Size of double: %zu\n", sizeof(double));              // 8
    printf("Size of s1 (array[10]): %zu\n", sizeof(s1));          // 10
    printf("Size of s2 (array[6]): %zu\n", sizeof(s2));           // 6
    printf("Size of s3 (pointer): %zu\n", sizeof(s3));            // 8 (64-bit) / 4 (32-bit)
    printf("Size of string literal: %zu\n", sizeof("Hello"));     // 6 (รวม \0)
    
    return 0;
}

ผลลัพธ์ (64-bit System)

Size of variable a (int): 4
Size of int: 4
Size of char: 1
Size of float: 4
Size of double: 8
Size of s1 (array[10]): 10
Size of s2 (array[6]): 6
Size of s3 (pointer): 8
Size of string literal: 6

ตารางเปรียบเทียบสำคัญ

ด้านเปรียบเทียบstrlen()sizeof()
สิ่งที่วัดString Length (Logical)Object Size in Bytes (Physical)
Null Terminatorไม่นับนับ (ถ้าเป็น Array Init ด้วย String Literal)
เวลาทำงานRuntime (O(n) - ต้อง Scan หา \0)Compile Time (Constant)
ParameterPointer to CharType หรือ Expression
Pointer Decayทำงานได้ (รับ Pointer)ให้ขนาด Pointer (ไม่ใช่ Array Size)
Header<string.h>Built-in Operator (ไม่ต้อง Include)

จุดสำคัญ: Array vs Pointer

char arr[] = "Hello";   // Array of 6 chars
char *ptr = "Hello";    // Pointer to String Literal (Read-only)

sizeof(arr)  // 6  (Array Size)
sizeof(ptr)  // 8  (Pointer Size - 64-bit)
strlen(arr)  // 5  (String Length)
strlen(ptr)  // 5  (String Length)

ข้อผิดพลาดที่พบบ่อย

// ผิด: ใช้ sizeof กับ Pointer
char *str = malloc(100);
strcpy(str, "Hello");
printf("%zu\n", sizeof(str));  // ผลลัพธ์: 8 (Pointer Size!) ไม่ใช่ 100

// ถูกต้อง: เก็บ Size แยก หรือใช้ strlen
size_t size = 100;
printf("%zu\n", strlen(str));  // 5 (String Length)
// หรือ
printf("%zu\n", size);         // 100 (Allocated Size)

สรุป: เมื่อไหร่ควรใช้อะไร

  • strlen(): ต้องการความยาว String (Logical Length) - Loop, Copy, Compare, Print
  • sizeof(): ต้องการขนาดหน่วยความจำ (Bytes) - malloc, memcpy, Buffer Size, Stack Allocation
  • Array Parameter ในฟังก์ชัน: sizeof ให้ Pointer Size → ต้องส่ง Size มาแยกต่างหาก