You should assign an array of char pointers, and then, for each pointer assign enough memory for the string:

char **orderedIds;

orderedIds = malloc(variableNumberOfElements * sizeof(char*));
for (int i = 0; i < variableNumberOfElements; i++)
    orderedIds[i] = malloc((ID_LEN+1) * sizeof(char)); // yeah, I know sizeof(char) is 1, but to make it clear...

Seems like a good way to me. Although you perform many mallocs, you clearly assign memory for a specific string, and you can free one block of memory without freeing the whole "string array"

Answer from MByD on Stack Overflow
🌐
Reddit
reddit.com › r/learnprogramming › how to dynamically allocate an array and add strings to it? (in c)
r/learnprogramming on Reddit: How to dynamically allocate an array and add strings to it? (In C)
February 17, 2022 -

Okay so basically I have a function that takes in a string and counts the lowercase tokens in it. I need to make a function that then returns an array of the lowercase tokens. I would need to use malloc to allocate enough memory for such array but I don’t know how to go about doing so.

Once the array is allocated how would I put the token strings into the array?

Any help is appreciated thank you

Discussions

C programming 2d dynamic string array
Hello and happy new year everyone ! I have to do this project for Christmas and my time is running out... The project is to make an English to Greek dictionary and in order to do that i need a 2D dynamic array. This is what i've come up with: #include #include #include #include #define s_size ... More on forum.level1techs.com
🌐 forum.level1techs.com
0
0
January 9, 2016
Without malloc, how is the array of strings making use of dynamic memory allocation?
That example simply prints the three strings in the static array arr[0-2]. To do this dynamically, you would malloc a block of memory and then write C strings to it while maintaining/saving pointers to the first character of each string. More on reddit.com
🌐 r/cprogramming
3
0
June 22, 2023
dynamic array of strings - C++ Forum
I just have a couple of questions on how to use dynamic arrays. First let me say I don't know how to use pointers or arrays, etc.. but below is the abbreviated version of my code, so.. I created a pointer to strings called stringTerms, then pass it to the function process to create a new ... More on cplusplus.com
🌐 cplusplus.com
August 15, 2022
Dynamic String in array - C++ Forum
By “dynamically” I assume you mean dynamically-allocated char* strings? And not std::string and std::vector? There are several basic possibilities for structure here (and multiple ways to accomplish one of them), and your post is not sufficiently specific to distinguish them. I suspect that what is wanted, though, is explained here: (How do I declare a 2d array ... More on cplusplus.com
🌐 cplusplus.com
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-create-a-dynamic-array-of-strings-in-c
How to Create a Dynamic Array of Strings in C? - GeeksforGeeks
July 23, 2025 - To 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.
🌐
Quora
quora.com › In-C-programming-how-do-you-create-a-dynamic-string-array-and-perform-string-operations-with-elements-of-the-array-and-a-constant-string
In C programming, how do you create a dynamic string array and perform string operations with elements of the array and a constant string? - Quora
Answer (1 of 2): In C a string is simply an array of characters (char) with a NULCHAR ([code ]'\0'[/code]) as the last character. That is all it is. C string functions all key off this NULCHAR at the end, for their operations, and each function ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › dynamic-array-in-c
Dynamic Array in C - GeeksforGeeks
July 23, 2025 - Array in C is static in nature, so its size should be known at compile time and we can't change the size of the array after its declaration. Due to this, we may encounter situations where our array doesn't have enough space left for required elements or we allotted more than the required memory leading to memory wastage. To solve this problem, dynamic arrays come into the picture.
🌐
Level1Techs
forum.level1techs.com › dev zone › code
C programming 2d dynamic string array - Code - Level1Techs Forums
January 9, 2016 - Hello and happy new year everyone ! I have to do this project for Christmas and my time is running out... The project is to make an English to Greek dictionary and in order to do that i need a 2D dynamic array. This is what i've come up with: #include #include #include #include #define s_size 256 void show_menu(); unsigned int Add_El( char**, char**,unsigned int); void save_file( char**, char**,unsigned int); int main() { unsigned int Sum_of_Term...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_array_of_strings.htm
Array of Strings in C
To declare an array of strings, you need to declare a two-dimensional array of character types, where the first subscript is the total number of strings and the second subscript is the maximum size of each string.
🌐
Medium
medium.com › @sohaib.arshid101 › dynamic-arrays-in-c-an-implementation-guide-4a959de94332
Dynamic arrays in C: An implementation guide | by curious&dumb | Medium
March 5, 2024 - Since there were many iterations involved in implementing the dynamic array interface, this post needs to be divided into sections. ... Arrays in C are static in nature. It means that whenever you create an array in C with something like this ... It creates a memory block of five elements of size int on the stack.
🌐
Programiz
programiz.com › c-programming › c-dynamic-memory-allocation
C Dynamic Memory Allocation Using malloc(), calloc(), free ...
Sometimes the size of the array you declared may be insufficient. To solve this issue, you can allocate memory manually during run-time. This is known as dynamic memory allocation in C programming.
🌐
Reddit
reddit.com › r/cprogramming › without malloc, how is the array of strings making use of dynamic memory allocation?
r/cprogramming on Reddit: Without malloc, how is the array of strings making use of dynamic memory allocation?
June 22, 2023 -

While I can understand the below is an example of static memory allocation while working on string arrays (2-dimensional character arrays): https://preview.redd.it/rndsb5zpgj7b1.png?width=1600&format=png&auto=webp&v=enabled&s=84cfe98ba875fee94947163de2b05ffb8c94650c
In order to optimize, dynamic memory allocation is the way in which for each string literal once \0 encountered, that marks the end of that string. Malloc should find a way into the code of such programs. However, I could not see the usage of Malloc or stdlib.h library in the below example: https://preview.redd.it/g44301m9hj7b1.png?width=1600&format=png&auto=webp&v=enabled&s=a29543a7a335f71c77d9014195661e7e9ef9ac30
Source: https://www.geeksforgeeks.org/array-of-strings-in-c/

🌐
Cplusplus
cplusplus.com › forum › beginner › 30599
dynamic array of strings - C++ Forum
August 15, 2022 - And kyle11778, thanks for your response as well.. stringTerms.size() did not work for me. it is a pointer to an array of strings.. so, i dont know how that changes being able to determine its size..? Anyone out there know how to determine the number of elements in an array of strings?? ... If the array was dynamically allocated, there is no way.
🌐
Quora
quora.com › How-can-I-allocate-a-string-array-dynamically-in-C++
How to allocate a string array dynamically in C++ - Quora
Answer (1 of 2): we can create easily using stl functions .Here is the code #include #include #include a(n); for(int i=0;i
🌐
Linux Hint
linuxhint.com › create-array-strings-using-malloc-c-programming
How to Create an Array of Strings Using Malloc() in C Programming – Linux Hint
The “malloc()” function multiplies the character’s size by a number of character bytes to allocate sufficient memory blocks. The pointer array “str_array[i]” is used to point and store strings in the array. Lastly, the “for” loop is used to iterate the array. After that, we printed the array using the “printf” function. ... It can be observed that we have successfully created a dynamic array of strings using the “malloc()” function.
🌐
Quora
quora.com › How-do-I-create-an-array-of-strings-in-c++-dynamically-using-pointers
How to create an array of strings in c++ dynamically using pointers - Quora
Answer (1 of 9): Just like you create an dynamic array of int type, you can also create an array of string which is nothing but of type const char* type in C/C++. As like we make an array of int, we create a pointer of int* type, so for string which is const char* type, we make pointer of const ...
🌐
Wikiversity
en.wikiversity.org › wiki › C_Programming › Strings
C Programming/Strings - Wikiversity
There is no string type in C. Instead we have arrays or constants, and we can use them to store sets of characters. The string "Hello World!" can be stored like in the example below · This creates an array of chars so that s[0] is 'H', s[1] is 'e', etc. In addition, because C allows us to ...
🌐
W3Schools
w3schools.com › c › c_memory_struct.php
C Structures and Dynamic Memory
If you need more elements later, you can resize your dynamic array with realloc(). This may move the block to a new location and returns a new pointer. Always store the result in a temporary pointer first to avoid losing the original memory if reallocation fails. #include <stdio.h> #include <stdlib.h> #include <string.h> struct Car { char brand[50]; int year; }; int main() { int count = 2; struct Car *cars = (struct Car*) malloc(count * sizeof(struct Car)); if (cars == NULL) { printf("Initial allocation failed.\n"); return 1; } // Initialize first 2 cars strcpy(cars[0].brand, "Toyota"); cars[0
🌐
Sololearn
sololearn.com › en › Discuss › 3037708 › im-confused-on-how-to-add-strings-to-a-dynamically-array-in-c
I'm confused on how to add strings to a dynamically array in c
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
W3Schools
w3schools.com › c › c_memory_allocate.php
C Allocate Memory
C has two types of memory: Static memory and dynamic memory. Static memory is memory that is reserved for variables before the program runs. Allocation of static memory is also known as compile time memory allocation. C automatically allocates memory for every variable when the program is compiled. For example, if you create an integer array ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 251410
Dynamic String in array - C++ Forum
I suggest a two-pass approach: (1) Count the number of words. Allocate the pointer array. (2) Read each word and allocate the sub-arrays (strings). This presumes an additional 1D array used to obtain the entire string (all words at once) from the user before collecting it into individual pieces. If you must read one word at a time, you can assume a smaller input array for reading from the user (50 characters should suffice for Basic Latin-1 text), then dynamically resize your pointer array each time you get a new word.