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?

DaedalusUsedPerl
  • 772
  • 5
  • 9
  • 25

1 Answers1

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.

john
  • 85,011
  • 4
  • 57
  • 81