Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 5
    No, there isn't. Open your C++ book to the chapter that explains how to use std::vector, for a more complete explanation of how this needs to be done. Commented Nov 17, 2019 at 22:42
  • C has a realloc but you would need to use the malloc or calloc C function to allocate the array in the first place. Using C functions is for backwards compatibility only and it isn't best practice for a new C++ program to use them. The comment about using std::vector is good advice. Commented Nov 17, 2019 at 22:58
  • @SamVarshavchik Of course there's a realloc function in c++. You can't use it to reallocate an automatic variable (a variable allocated on the stack, as OP appears to be doing it) but you could definitely allocate an array of strings, and then reallocate it. (It's messier than just using a std::vector, but OP's instructor may be teaching the old-fashioned messy way first, before showing the easy, modern way.) Commented Nov 17, 2019 at 23:03
  • 1
    @DaveM. -- try to use malloc and realloc with std::strings, and see how well it works out for you. Commented Nov 17, 2019 at 23:04
  • 1
    Possible duplicate of Create an array when the size is a variable not a constant Commented Nov 17, 2019 at 23:10