0

My function has this prototype:

char[][100] toArray(char document[]);

g++ on cygwin returns this error:

Unable to resolve identifier toArray

How do I return an array of C-Strings?

1
  • Do you really need to return an array of C strings, or is this a possible solution to a greater problem? Commented Nov 17, 2012 at 9:03

1 Answer 1

6

It's impossible to return an array in C++. The closest you can do is return a pointer to a dynamically allocated block of strings.

This is legal code

typedef char str100[100];

str100* toArray(char* document)
{
    str100 *block = new str100[20];
    return block;
}

The typedef makes it a little easier to understand. If you don't believe me here's the same code without the typedef

char (*toArray(char* document))[100]
{
    char (*block)[100] = new char[20][100];
    return block;
}

This is meant to scare you.

But although this code is legal it is also rubbish. You should be using std::vector<std::string>. Dynamically allocating memory is hard, much harder than using classes that do the work for you.

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

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.