Is it possible in C++ to have a function that returns a 2-D or indeed an X-D array from a function, with a specified type such as..

char [][] myFunction()
{
   char a[][];
   //Do something with array here

   return a;
}

Also, if this can be done, can the array returned by that of one has not been specified a size in its declaration.

No, but you can return a pointer pointing to your X-D array through which you can then access the elements from the calling function.

>Is it possible in C++ to have a function that returns a 2-D or indeed an
>X-D array from a function, with a specified type such as.
No. As already said, you can return a simulated multidimensional array through the use of pointers, but there's no array syntax for return values. A better option is to make use of standard containers rather than arrays or dynamic arrays. For example:

// Return a two-dimensional array in the form of a vector of vectors
vector<vector<int> > myFunction();

I see.. well with that aside if I had the following declaration..

char * szString[] = { };

how would I go about filling the array with char*'s?
I have this so far..

char* string0[]={};
char* string1 = "Hello";
char* string2 = "World.";

string0[0] = string1;
string0[1] = string2;

>char * szString[] = { };
This shouldn't compile. You can't combine an empty size with an empty initialization list.

>how would I go about filling the array with char*'s?
It depends on what you plan to do with them. So, what are you trying to accomplish with this array of pointers?

>char * szString[] = { };
This shouldn't compile. You can't combine an empty size with an empty initialization list.

I tried declaring it without an initialization list as

char* szString[];

but the compiler came up with an error: "storage size of string0[] isn't known"

Eventually what I am doing is reading a file that contains lines of formatted information in this format..

{00}{01}Text.

I want to read the entire file and store each line in an array of c-strings like char* string0[]; where string[linenumber] contains the line corresponding to linenumber. i.e. string0[0] contains "{00}{01}Text." etc
originally I wanted to do this using a function and return the value, which was the point of this thread but now that I can't do that, I am trying to read from the file and store everything line by line into this array of c-strings.

I have already written the functions used to interpret each line and split it up into the different sections and these functions use c-strings, so if possible I would prefer to keep using c-strings.

>but the compiler came up with an error: "storage size of string0[] isn't known"
You have to give an array a size. It's really that simple. You can omit the size of you use a non-empty initialization list because the size will be automatically taken from the size of the list.

>I want to read the entire file and store each line in an array of c-strings
Okay, then you'll want to allocate memory to each line equivalent to the length of the string contained in the buffer you're using to read the line:

string buffer;
char *lines[N];

for ( int i = 0; i < N && getline ( in, buffer ); i++ ) {
  lines[i] = new char[buffer.size() + 1];
  strcpy ( lines[i], buffer.c_str() );
}

However, the big problem here is what value to give to N. Do you know ahead of time the number of lines in the file? If not, you shouldn't use an array. A vector would be best, or you'll end up having to deal with reallocation of your dynamic array, which is a pain in the butt.

Oh right thanks. You know I seem to tackle these problems the hardest possible way, I tend to mix up C & C++ which just in the end gets a bit confusing and I can see why I shouldn't. I'll have a read up on vectors and such, but for now do you have any decent references on dealing with c-strings online?

Thanks.

This article has been dead for over six months. Start a new discussion instead.