Is there a way to create a dynamic array without necessarily knowing the size beforehand? I need to specifically use a dynamic array for a homework assignment, and I don't really want to ask the user how large the array should be (in this case, an array of chars i.e. a string).

Recommended Answers

All 2 Replies

You have to supply a size, but it can be a "best guess" and you can always reallocate later if the guess was wrong. If it's user input from the console, I imagine a buffer of 100 would probably do the job without reallocation in most cases.

OK. I was reading somewhere that you can get the length of the string (characters) input using gcount. Can I do that before reallocating the memory for the array or do I have to use another method to reallocate?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.