Differences Between char*, Character Arrays, and Dynamically Allocated Strings in C Programming

This is a very critical and often confused knowledge point in C language: <span>char*</span>, character arrays (such as <span>char str[]</span>), and the relationship between strings. Let’s thoroughly clarify their essence, differences, and connections.

1. Basic Concepts of the Three

Term Meaning
<span>char*</span> Character pointer, pointing to the address of one (or more) <span>char</span> type variables
<span>char str[]</span> Character array, where each element is a <span>char</span>, and the array itself allocates memory
String constant A segment of characters enclosed in double quotes, such as <span>"hello"</span>, which is a character array constant ending with <span>\0</span>.

2. Code Comparison Examples

Example 1: Using Character Arrays

char str1[] = "hello";
  • <span>str1</span> is a character array with a length of 6 (including <span>\0</span>)
  • It allocates memory on the stack, with content <span>{'h','e','l','l','o','\0'}</span>

Example 2: Using Pointer to Point to String Constant

char* str2 = "hello";
  • <span>str2</span> is a pointer pointing to the address of the string constant <span>"hello"</span>.
  • The string constant may be in the read-only segment, and cannot be modified

Example 3: Manually Allocating String Space

char* str3 = malloc(6);
strcpy(str3, "hello");
  • <span>str3</span> is a pointer pointing to a block of heap memory allocated by <span>malloc</span>.
  • The content is writable, but must be <span>free</span>d.

3. Illustrated Understanding (Logical Structure)

char str1[] = "hi";   // Stack: ['h','i','\0']
char* str2 = "hi";    // Pointer: str2 ──► Read-only memory "hi\0"

4. Comparison of Common Operations

Operation <span>char str[]</span> <span>char* str</span> (String Constant)
Memory Allocation Yes No, points to constant area
Modifiable ✅ Modifiable ⚠️ Not recommended to modify
Usage Type Stack variable Pointer reference

5. Examples of Operational Differences

✅ Modifying Character Array Content (Valid)

char str[] = "hi";
str[0] = 'H';  // ✅ OK

⚠️ Modifying String Constant Content (Undefined Behavior)

char* str = "hi";
str[0] = 'H';  // ⚠️ Error: Modifying constant area, may crash

6. Passing Arrays vs Pointers

void print1(char str[]) {
    printf("%s\n", str);
}

void print2(char* str) {
    printf("%s\n", str);
}

For the compiler, <span>char str[]</span> in parameters will decay to <span>char*</span>, so these two functions are actually equivalent.

7. Summary of Common Usage Comparisons

Scenario Recommended Usage
Read-only String <span>const char* str = "hello";</span>
String Needs Modification <span>char str[] = "hello";</span>
Dynamically Created String <span>char* str = malloc(...)</span>
Function Returns String Return <span>char*</span> or <span>const char*</span>

8. A Complete Example

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

int main() {
    char arr[] = "hello";      // Stack array
    char* ptr = "world";       // Pointer pointing to read-only string
    char* dyn = malloc(6);     // Heap space
    strcpy(dyn, "again");

    arr[0] = 'H';              // OK
    // ptr[0] = 'W';           // ⚠️ Unsafe
    dyn[0] = 'A';              // OK

    printf("%s\n", arr);       // Hello
    printf("%s\n", ptr);       // world
    printf("%s\n", dyn);       // Again

    free(dyn);
    return 0;
}

9. Key Summary

Item <span>char str[] = "hi";</span> <span>char* str = "hi";</span>
Memory Allocation Stack Static read-only area
Writable ✅ Yes ❌ No (Undefined Behavior)
Is Array Yes No (just a pointer)
sizeof Length + 1 (including <span>\0</span>) Pointer size (e.g., 8 bytes)

Leave a Comment