Memory Allocation and String Manipulation in C: Arrays vs Pointers

In C programming, handling strings requires a clear understanding of how memory is allocated. Strings can be managed using either character arrays or character pointers, each behaving differently regarding memory size and data manipulation.

1. String Manipulation Using Character Arrays

When using a character array, memory is statically allocated. The sizeof operator returns the total allocated size of the buffer, whereas strlen calculates the number of characters currently stored before the null terminator.

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

#define BUFFER_SIZE 80

int main() {
    char first_str[BUFFER_SIZE] = "Learning makes me happy";
    char second_str[BUFFER_SIZE] = "Learning makes me sleepy";
    char temp_buffer[BUFFER_SIZE];

    // Comparing memory size vs string length
    printf("Array Size Metrics:\n");
    printf("sizeof(first_str) = %zu bytes\n", sizeof(first_str));
    printf("strlen(first_str) = %zu characters\n", strlen(first_str));

    printf("\nOriginal State:\n");
    printf("1: %s\n2: %s\n", first_str, second_str);

    // Swapping content using strcpy
    strcpy(temp_buffer, first_str);
    strcpy(first_str, second_str);
    strcpy(second_str, temp_buffer);

    printf("\nAfter Swapping Content:\n");
    printf("1: %s\n2: %s\n", first_str, second_str);

    return 0;
}

Technical Analysis:

  • Size Comparison: sizeof(first_str) returns 80 (the total capacity), while strlen(first_str) returns the count of visible characters.
  • Assignment: Arrays cannot be reassigned directly (e.g., s1 = s2 is invalid). Data must be moved using memory copying functions like strcpy.
  • Swapping: The actual characters are moved between different memory locations in the stack.

2. String Management Using Character Pointers

Character pointers store the memory address of the first character of a string literal. Swapping pointers is significantly more efficient than copying array contents because only the memory addresses are exchanged.

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

int main() {
    char *ptr1 = "Learning makes me happy";
    char *ptr2 = "Learning makes me sleepy";
    char *temp_ptr;

    // Comparing pointer size vs string length
    printf("Pointer Size Metrics:\n");
    printf("sizeof(ptr1) = %zu bytes\n", sizeof(ptr1));
    printf("strlen(ptr1) = %zu characters\n", strlen(ptr1));

    printf("\nOriginal State:\n");
    printf("ptr1: %s\nptr2: %s\n", ptr1, ptr2);

    // Swapping addresses
    temp_ptr = ptr1;
    ptr1 = ptr2;
    ptr2 = temp_ptr;

    printf("\nAfter Swapping Addresses:\n");
    printf("ptr1: %s\nptr2: %s\n", ptr1, ptr2);

    return 0;
}

Technical Analysis:

  • Size Comparison: sizeof(ptr1) returns the size of the pointer itself (usually 4 or 8 bytes depending on the architecture), not the length of the string it points to.
  • Efficiency: Unlike arrays, swapping strings via pointers does not move the text in memory; it merely updates the pointers to look at different memory addresses.

3. Application: Sorting Strings via Command-Line Arguments

Using the logic of pointer manipulation, we can sort an array of strings (like command-line arguments) efficiently using string comparison and pointer swapping.

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

/**
 * Sorts an array of string pointers alphabetically
 * @param count The number of strings
 * @param strings Array of character pointers
 */
void sort_strings(int count, char *strings[]) {
    int i, j;
    char *temp;

    for (i = 0; i < count - 1; i++) {
        for (j = 0; j < count - 1 - i; j++) {
            // Compare strings lexicographically
            if (strcmp(strings[j], strings[j + 1]) > 0) {
                // Swap pointers, not content
                temp = strings[j];
                strings[j] = strings[j + 1];
                strings[j + 1] = temp;
            }
        }
    }
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: %s [string1] [string2] ...\n", argv[0]);
        return 1;
    }

    // Sort arguments excluding the program name
    sort_strings(argc - 1, &argv[1]);

    printf("Sorted Arguments:\n");
    for (int i = 1; i < argc; i++) {
        printf("Hello, %s\n", argv[i]);
    }

    return 0;
}

The sort_strings function implements a bubble sort algorithm. It utilizes strcmp to determine the alphabetical order and then swaps the memory addresses stored in the argv pointer array to reorder the input values without mdoifying the original data in the environment block.

Tags: c-programming pointers memory-management string-manipulation algorithms

Posted on Sat, 25 Jul 2026 17:09:06 +0000 by ridckie_rich