How to Create a Dynamic Array of Strings in C? Last Updated : 22 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the program as per the user's needs. In this article, we will learn how to create a dynamic array of strings in C. Create a Dynamic Array of Strings in CTo create a dynamic array of strings in C, we can use the concept of double pointer and dynamic memory allocation. The double pointer is the pointer that stores the memory address of another pointer. We create an array of pointers to characters (i.e. strings) and then store the address of this array in the double-pointer to characters. Each of the pointer to the character is again allocated memory based on the size of the string it is storing. ApproachInitialize a double pointer to store an array of strings.Allocate memory for the initial size of the array using the malloc function.Each element in the array will be a pointer to a string.For each pointer in the array, allocate memory for the string using the malloc function.Use the sprintf function to assign value to the string in the array.Free memory for each string using the free function.Free memory for the whole array of pointers using the free function.Note: We have to first free() the memory allocated to each of the pointer of the array and then free the array of pointers. Otherwise, it may lead to the memory leak. C Program to Create a Dynamic Array of StringsThe following program illustrates how to create a dynamic array of strings in C: C // C Program to illustrate how to create a dynamic array of // strings #include <stdio.h> #include <stdlib.h> #include <string.h> // define the maximum length of the string you will store in // the array #define MAX_STRING_LENGTH 20 int main() { // Initialize a double pointer to store the array of // strings char** strings = NULL; // Declare the initial size of the dynamic array int size = 5; // Allocate memory for the array of strings strings = (char**)malloc(size * sizeof(char*)); if (strings == NULL) { fprintf(stderr, "Memory allocation failed\n"); return 1; } // Allocate memory for each string and assign values for (int i = 0; i < size; i++) { // Allocate memory for each string strings[i] = (char*)malloc((MAX_STRING_LENGTH + 1) * sizeof(char)); if (strings[i] == NULL) { fprintf(stderr, "Memory allocation failed\n"); return 1; } // Assign values to each string sprintf(strings[i], "Student%d", i); } // Print the strings present in the array for (int i = 0; i < size; i++) { printf("%s\n", strings[i]); } // Free memory for each string for (int i = 0; i < size; i++) { free(strings[i]); } // Free memory for the array of pointers free(strings); return 0; } OutputStudent0 Student1 Student2 Student3 Student4 Time Complexity: O(N), where N is the number of strings in the array.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Create a Dynamic Array of Strings in C? 21211a4uyj Follow Improve Article Tags : C Programs C Language C-Arrays C-Dynamic Memory Allocation C Strings Programs C Examples +2 More Similar Reads C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used 5 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Like