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:
- We declare a pointer
char *strto hold the dynamically allocated memory. - The
malloc()function allocates memory for 20 characters and returns a pointer to the allocated memory. - We check if
malloc()returnedNULL, indicating allocation failure. - The
strcpy()function copies the string “Hello, World!” into the allocated memory. - The
printf()function prints the string. - 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:
- The pointer
char *stris declared to store dynamically allocated memory. - The
calloc()function is used to allocate memory for 50 characters, initializing them to zero. - We check if
calloc()returnedNULL, indicating memory allocation failure. - The
fgets()function reads input from the user and stores it in the allocated memory. - The
printf()function prints the user input. - 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:
- The pointer
char *stris used to store the dynamically allocated memory. - We allocate 10 bytes using
malloc()and copy “Hello” into it. - We print the string before reallocation.
- The
realloc()function increases the allocated memory to 30 bytes. - We check if
realloc()returnedNULL, indicating failure. - The
strcat()function appends “, C Language!” to the existing string. - We print the modified string after reallocation.
- The
free()function releases the allocated memory.
Output:
Before reallocation: Hello
After reallocation: Hello, C Language!
