Differentiate Between Character Arrays and Pointers in Strings in C

In C, character arrays and pointers to strings appear similar but have distinct behaviors in memory allocation, mutability, and function usage. A character array is a fixed-size storage for a string, while a pointer to a string references memory dynamically.


Examples to Differentiate Between Character Arrays and Pointers

1. Modifying a String: Character Array vs. String Literal

In this example, we will see how modifying a string works differently for a character array and a pointer to a string literal.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char arr[] = "Hello";
    char *ptr = "Hello";

    // Modifying character array
    arr[0] = 'M'; 
    printf("Modified array: %s\n", arr);

    // Attempting to modify string literal (causes error)
    // ptr[0] = 'M'; // Uncommenting this will cause a segmentation fault

    return 0;
}

Explanation:

  1. The character array arr stores the string "Hello" in stack memory, allowing modifications.
  2. The pointer ptr points to a string literal stored in read-only memory, making modifications illegal.
  3. Modifying arr[0] to 'M' works, but modifying ptr[0] would cause a segmentation fault.

Output:

Modified array: Mello

2. Using sizeof vs. strlen on Character Arrays and Pointers

In this example, we will see how sizeof and strlen behave differently for character arrays and string pointers.

main.c

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

int main() {
    char arr[] = "Hello";
    char *ptr = "Hello";

    printf("sizeof(arr): %lu\n", sizeof(arr));
    printf("sizeof(ptr): %lu\n", sizeof(ptr));
    printf("strlen(arr): %lu\n", strlen(arr));
    printf("strlen(ptr): %lu\n", strlen(ptr));

    return 0;
}

Explanation:

  1. sizeof(arr) gives the total allocated memory for the array, including the null character.
  2. sizeof(ptr) gives the size of the pointer (usually 4 or 8 bytes), not the string.
  3. strlen(arr) returns the number of characters in the string, excluding the null character.
  4. strlen(ptr) behaves the same way as strlen(arr) since both reference the same string content.

Output:

sizeof(arr): 6
sizeof(ptr): 8
strlen(arr): 5
strlen(ptr): 5

3. Passing Strings to Functions

In this example, we will see how character arrays and pointers behave when passed to functions.

main.c

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

void modifyString(char str[]) {
    str[0] = 'M';
}

int main() {
    char arr[] = "Hello";
    modifyString(arr);
    printf("Modified array: %s\n", arr);

    return 0;
}

Explanation:

  1. The function modifyString accepts a character array, which decays into a pointer.
  2. Modifying str[0] inside the function changes the original string.
  3. Since arrays are passed as pointers, the modification is reflected in main().

Output:

Modified array: Mello

Conclusion

In this tutorial, we explored the differences between character arrays and string pointers in C:

  1. Character arrays allow modification, whereas string literals accessed via pointers are immutable.
  2. The sizeof operator works differently for arrays and pointers.
  3. When passed to functions, character arrays behave like pointers.