int main(){
string array[][]=get_element(2,3);
return 0;
}

string get_element(int numrow,int numcol){
	string result_array[numrow][numcol];

	for (int i=0;i<numrow;i++)
	{
		for (int j=0;j<numcol;j++)
		{
			result_array[i][j]=" ";
		}
	}

return result_array; //ERROR is on this line
}

I get error: conversion from ‘std::string (*)[(((long unsigned int)(((long int)numcol) + -0x00000000000000001)) + 1)]’ to non-scalar type ‘std::string’ requested

I must be missing something, how do I properly return a 2-d string array from a function?

Thanks in advance

From what I see , you do not need to return a 2d array. Consider just
doing this :

void set_element(string array2D[][MAX_COL], const int MAX_ROW)
{	
	for (int i = 0 ; i < MAX_ROW ; i++)
	{
		for (int j = 0 ; j < MAX_COL ; j++)
		{
			array2D[i][j]=" ";
		}
	}
}

I agree with firstPerson. However, I'm confused as to why this function marches along initializing some of the array members and then stops. You are asking it to "get" an element but returning a whole array. Unless you are doing some error checking that isn't evident in your code why wouldn't you use arr[row][col]? I'm just puzzled is all....

The above code is a simplified version of what I am trying to do.

After initializing that array, it will be filled with data from a file (numrow in the actual program is computed from the file itself instead of a parameter of the function), and then returns that array.

and since this function needs to be general enough to take any files thats in a tabula form...so...it'd be neat if I can return a 2-d array

>>it'd be neat if I can return a 2-d array

It would also be slow and maybe dangerous depending on some things.

Why not just pass in a 2d array, and fill it with the data from the file?

I must be missing something, how do I properly return a 2-d string array from a function?

you could do something like

typedef string (*StrArrPtr)[numCols];

StrArrPtr get_elements()
{

   //Do your stuff, string arr[numrows][numcols]

 return arr;
}

words of caution: Make sure you are not returning address of a local array variable.
and try avoiding using arrays in general, use vectors if you can.

There shouldn't be anything wrong with returning a 2-dim array.
Something like the following should work.

#include <iostream>
#include <string>

std::string **getstring(int x, int y){
  std::string **var = new std::string*[x];
  for (int i=0;i<x;i++)
    var[i]=new std::string[y];
  return var;
}

int main(){
  std::string **tmp = getstring(5,10);
  return 0;
}

This assumes the same number of elements at each line, and you should cleanup the memory.

good luck

To firstPerson

If I want to string array2D[][MAX_COL] as a parameter in the function

in main() how would I declare that variable?
string array2D[][Max_COL] // gives error saying undetermined size

and how would I initialize that function at the beginning of the code?


Thanks

There shouldn't be anything wrong with returning a 2-dim array.
Something like the following should work.

#include <iostream>
#include <string>

std::string **getstring(int x, int y){
  std::string **var = new std::string*[x];
  for (int i=0;i<x;i++)
    var[i]=new std::string[y];
  return var;
}

int main(){
  std::string **tmp = getstring(5,10);
  return 0;
}

This assumes the same number of elements at each line, and you should cleanup the memory.

good luck

This works like a charm. Thanks!!

Can you explain the meaning of **getstring(int x, int y), the two **?

To firstPerson

If I want to string array2D[][MAX_COL] as a parameter in the function

in main() how would I declare that variable?
string array2D[][Max_COL] // gives error saying undetermined size

and how would I initialize that function at the beginning of the code?

Thanks

Like this :

const int ROW = 5;
const int COL = 5;
void foo(string array2d[ROW][COL]){
}
int main()
{
    string mainArray[ROW][COL];
     foo(mainArray);
}

This works like a charm. Thanks!!

Can you explain the meaning of **getstring(int x, int y), the two **?

There is a relationship between pointer and arrays.
so int a[] can be written as int *a,
so a double array int a[][], can be written as int **a.

This is simlified, but its the general idea.
good luck :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.