Dynamically Allocate Memory for Strings in C

In C, we can dynamically allocate memory for strings using functions like malloc(), calloc(), and realloc() from the stdlib.h library. Dynamic memory allocation allows us to allocate memory at runtime, making it useful when handling variable-length strings or input from the user.


Examples of Dynamic Memory Allocation for Strings

1. Allocating Memory for a String Using malloc()

In this example, we will allocate memory dynamically for a string using the malloc() function, store a string in it, and print the string.

main.c

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

int main() {
    char *str;

    // Allocating memory dynamically for a string of 20 characters
    str = (char *)malloc(20 * sizeof(char));

    if (str == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Copying a string into allocated memory
    strcpy(str, "Hello, World!");

    // Printing the string
    printf("%s\n", str);

    // Freeing allocated memory
    free(str);

    return 0;
}

Explanation:

  1. We declare a pointer char *str to hold the dynamically allocated memory.
  2. The malloc() function allocates memory for 20 characters and returns a pointer to the allocated memory.
  3. We check if malloc() returned NULL, indicating allocation failure.
  4. The strcpy() function copies the string “Hello, World!” into the allocated memory.
  5. The printf() function prints the string.
  6. Finally, we use free() to release the allocated memory.

Output:

Hello, World!

2. Allocating Memory for a String Using calloc()

In this example, we will allocate memory for a string using calloc(), which initializes memory to zero, and then accept input from the user.

main.c

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

int main() {
    char *str;

    // Allocating memory for 50 characters using calloc
    str = (char *)calloc(50, sizeof(char));

    if (str == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Getting user input
    printf("Enter a string: ");
    fgets(str, 50, stdin);

    // Printing the entered string
    printf("You entered: %s", str);

    // Freeing allocated memory
    free(str);

    return 0;
}

Explanation:

  1. The pointer char *str is declared to store dynamically allocated memory.
  2. The calloc() function is used to allocate memory for 50 characters, initializing them to zero.
  3. We check if calloc() returned NULL, indicating memory allocation failure.
  4. The fgets() function reads input from the user and stores it in the allocated memory.
  5. The printf() function prints the user input.
  6. The free() function releases the allocated memory.

Output:

Enter a string: Hello World!
You entered: Hello World!

3. Reallocating Memory for a String Using realloc()

In this example, we will first allocate memory for a string using malloc() and then use realloc() to expand the memory size dynamically.

main.c

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

int main() {
    char *str;

    // Initial allocation of memory
    str = (char *)malloc(10 * sizeof(char));

    if (str == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    strcpy(str, "Hello");

    printf("Before reallocation: %s\n", str);

    // Reallocating memory to a larger size
    str = (char *)realloc(str, 30 * sizeof(char));

    if (str == NULL) {
        printf("Memory reallocation failed\n");
        return 1;
    }

    strcat(str, ", C Language!");

    printf("After reallocation: %s\n", str);

    // Freeing allocated memory
    free(str);

    return 0;
}

Explanation:

  1. The pointer char *str is used to store the dynamically allocated memory.
  2. We allocate 10 bytes using malloc() and copy “Hello” into it.
  3. We print the string before reallocation.
  4. The realloc() function increases the allocated memory to 30 bytes.
  5. We check if realloc() returned NULL, indicating failure.
  6. The strcat() function appends “, C Language!” to the existing string.
  7. We print the modified string after reallocation.
  8. The free() function releases the allocated memory.

Output:

Before reallocation: Hello
After reallocation: Hello, C Language!