char *array and char array[]

Learn char *array and char array[] with practical examples, diagrams, and best practices. Covers c, arrays, pointers development techniques with visual explanations.

Understanding char *array vs. char array[] in C

Hero image for char *array and char array[]

Explore the fundamental differences between character pointers and character arrays in C, their memory allocation, usage, and common pitfalls.

In C programming, char *array and char array[] are two common ways to declare variables that can hold sequences of characters, often referred to as strings. While they might appear similar at first glance, their underlying memory management, behavior, and appropriate use cases differ significantly. Understanding these distinctions is crucial for writing correct, efficient, and safe C code, especially when dealing with string manipulation and memory operations.

Character Arrays (char array[])

A char array[] declaration creates a contiguous block of memory on the stack (for local variables) or in the data segment (for global/static variables) that is large enough to hold the specified number of characters, plus a null terminator (\0) if initialized as a string literal. The size of the array is fixed at compile time or determined by the initializer. The array name itself is a constant pointer to the first element of the array; you cannot reassign it to point to a different memory location.

char greeting[] = "Hello"; // Array of 6 chars (H,e,l,l,o,\0)
char name[20];             // Array of 20 chars, uninitialized

// Assigning to an array (requires strcpy or similar)
strcpy(name, "World");

// Trying to reassign the array name (compile-time error)
// greeting = name; // ERROR!

Examples of char array[] declaration and usage.

graph TD
    A["char greeting[] = \"Hello\";"]
    B["Memory Block (Stack/Data Segment)"]
    C["greeting[0] = 'H'"]
    D["greeting[1] = 'e'"]
    E["..."]
    F["greeting[5] = '\\0'"]

    A --> B
    B --> C
    B --> D
    B --> E
    B --> F
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px
    style C fill:#ccf,stroke:#333,stroke-width:1px
    style D fill:#ccf,stroke:#333,stroke-width:1px
    style E fill:#ccf,stroke:#333,stroke-width:1px
    style F fill:#ccf,stroke:#333,stroke-width:1px

Memory representation of a char array[].

Character Pointers (char *array)

A char *array declaration creates a pointer variable that can hold the memory address of a character. This pointer itself is typically allocated on the stack (for local variables) or in the data segment (for global/static variables). The memory that the pointer points to can be allocated dynamically on the heap (using malloc, calloc), or it can point to a string literal in read-only memory, or even to the first element of an existing char array[]. Unlike array names, pointers are variables and can be reassigned to point to different memory locations.

char *message = "World"; // Pointer to a string literal (read-only memory)
char *buffer;              // Uninitialized pointer

// Allocate memory dynamically
buffer = (char *)malloc(sizeof(char) * 10);
strcpy(buffer, "Dynamic");

// Reassigning the pointer is allowed
message = buffer; // Now 'message' points to "Dynamic"

// Don't forget to free dynamically allocated memory
free(buffer);
buffer = NULL;

Examples of char *array declaration and usage.

graph TD
    A["char *message = \"World\";"]
    B["Pointer Variable 'message' (Stack)"]
    C["String Literal \"World\" (Read-Only Memory)"]
    D["char *buffer;"]
    E["Pointer Variable 'buffer' (Stack)"]
    F["Dynamically Allocated Memory (Heap)"]

    A --> B
    B --> C
    D --> E
    E -- "points to" --> F
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:2px
    style C fill:#ccf,stroke:#333,stroke-width:2px
    style D fill:#f9f,stroke:#333,stroke-width:2px
    style E fill:#bbf,stroke:#333,stroke-width:2px
    style F fill:#ccf,stroke:#333,stroke-width:2px

Memory representation of char *array pointing to different memory types.

Key Differences and Use Cases

The choice between char array[] and char *array depends heavily on whether you need a fixed-size buffer for mutable string data or a flexible pointer that can reference different string locations, including read-only literals. Understanding their memory characteristics is paramount to avoid common errors like buffer overflows or attempts to modify string literals.

Hero image for char *array and char array[]

Comparison of char array[] and char *array.