2

While performing a practice assignment online I came across a problem which I am not able to solve.

The user has to a number(number of sentences he will be entering) and then proceed to enter the sentences one by one, which are to be stored as strings(by the way, declaring a dynamic array of pointers is mandatory). However, since the number of sentences is not a priori deducible, I know that the size of the array of pointers actually is the number of sentences but I can't figure out how to declare a dynamic array of pointers to strings.

Using something I had already known beforehand, I figured out how to do the same but with arrays of characters, not arrays of strings. The line that declared a dynamic array of pointers to dynamic arrays of chars looked like this:

char **ptr=new char*[n] {};

So with my understanding, this creates a pointer ptr which points to a dynamic array of pointers, the elements of which each point to one array of characters. I want to do something similar now, where the result should be that ptr is a pointer to a dynamic array of pointers, the elements of which each point to a string.

Can anyone help out? I'd be thankful!

10
  • 1
    Are you willing to use a std::string, or do you want to do this in C with char pointers? Commented May 7, 2019 at 10:07
  • Yes, as I mentioned I have to use std::string for this one. Commented May 7, 2019 at 10:10
  • And by the way, I'm talking about C++, not C Commented May 7, 2019 at 10:11
  • So, you want something like std::string *ptr and new that Commented May 7, 2019 at 10:12
  • 1
    Gotcha and @doctorlove has got a pretty good approach to that one. If you need more strings, you will have to write a realloc scheme where you basically declare a newset of pointers (with double the number), copy old to new, delete[] old and then set old=newset and keep going. Commented May 7, 2019 at 10:34

2 Answers 2

2

I think what you are looking for is something like

std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::string *sentences = new std::string[num];
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/
delete[] sentences;     // free the memory

Note that this style of coding is highly discouraged. The problem is the need to manage the allocated memory: avoid memory leaks and dangling pointers (including exception safety). The correct approach is to use a container or smart pointer. For example:

std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::vector<std::string> sentences{num};
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/

or

std::size_t num;
std::cout << "enter the number of sentences\n";
std::cin  >> num;
std::unique_ptr<std::string[]> sentences{new std::string[num]};
for(std::size_t i=0; i!=num; ++i) {
    std::cout << "enter the " << (i+1) << "th sentence\n";
    std::cin  >> sentences[i];
}
/* 
    ... (do something with the sentences, accessing them as sentences[i])
*/

when in both cases you don't have to worry about calling delete: the allocated memory will be automatically deleted (even if an exception occurs).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that seemed to work. I have one more question though(if you have the time to reply). I think I may have come up with this on my own, but what confuses me is std::string *sentences = new std::string[num];. sentences is a pointer to strings, but new std::string[num] is something I haven't seen before. Isn't std::string internally a dynamically allocated array? What would new do in conjunction with string? Once again I am sorry for my ignorance, I am really new to this. Thank you for your immense help!
@I0ner9 If this answer was helpful, please upvote it and/or accept it. As to your additional question. This is standard operator new[] syntax, as in int *ptr = new int[42];, only that instead of int you allocate std::string. Yes, internally std::string may also use dynamic allocation to store long strings, but that does not matter here: the new operator only allocates the memory actually needed by the std::string, not the additional memory that the strings then may allocate.
2

You can avoid pointers completely and use

std::vector<std::string> input;

A std::array needs to know the size at compile time, and you learn this at runtime. The vector works like an array but can have items push_backed at runtime.

You could declare pointer to some strings, using n once you know it:

std::string * pInputs = new std::string[n];

but it's easier to use the vector. Each pInput will be a string, as with the std::vector version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.