How do I declare an array of strings with an unknown size?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you do not know in advance how many strings you need to store, a fixed-size array is usually the wrong first data structure. In many languages, arrays require a size at allocation time, which means you would otherwise have to guess, over-allocate, or keep copying into larger arrays as more strings arrive.
The usual solution is to collect values in a dynamic container first and convert to an array only if some later API really requires one. That pattern is simpler, safer, and usually faster than manual resizing.
Use a Dynamic Container First
Different languages solve this with different standard types:
- C# uses
List<string> - Java uses
ArrayList<String> - C++ uses
std::vector<std::string> - JavaScript arrays are already dynamic
The common idea is the same: grow the collection naturally while you discover the data, then materialize a final array only if you need one.
C# Example
In C#, the idiomatic answer is List<string>:
If the downstream API accepts IEnumerable<string> or List<string>, there may be no reason to convert to string[] at all.
Java Example
Java uses the same pattern with ArrayList:
Again, the important point is that the list grows dynamically while the final array is only created at the boundary where it is actually needed.
C++ Example
In C++, use std::vector<std::string>:
std::vector is the standard growable sequence container and is the natural replacement for a fixed-size string array when the size is unknown at first.
Low-Level Languages Need Manual Resizing
If you are working in plain C or another low-level environment, you may need to grow the array manually:
This works, but it shows why dynamic containers from standard libraries are usually the better first choice when they are available.
Common Pitfalls
The biggest mistake is allocating a fixed array too early and then repeatedly copying into larger arrays every time a new string arrives. That makes the code more complex than necessary.
Another common issue is converting the dynamic collection to an array inside a loop instead of only once at the API boundary. That creates needless allocations.
Developers also confuse reserved capacity with the current number of stored elements. A collection may have room for more items without actually containing them.
Finally, do not forget input cleanup. If strings come from users or files, it is often worth trimming or validating them before storing them in the growable container.
Summary
- If the number of strings is unknown, start with a dynamic container rather than a fixed array.
- Use
List<string>,ArrayList<String>, orstd::vector<std::string>depending on the language. - Convert to an array only when another API explicitly requires one.
- Low-level languages can resize manually, but that is more error-prone than standard dynamic containers.
- Keep collection growth and final array conversion as separate steps.

