0

is it possible to pass this to a function? it's size is configured at runtime.

for example,

void test(string s[][]) {

return;
}

int main() {
  int x, y;
  cin >> x >> y;
  string s[x][y];

  test(s);
return 0;
}
2
Contributors
1
Reply
6
Views
5 Years
Discussion Span
Last Post by firstPerson
1

Yes, but if you want to do it that way, you need to do manual allocation.

void foo(string** strs, int width, int height){
 //...
}
int main(){
 int width = 0, height = 0;
 cin >> height >> width;
 string **strs = new string*[height];
 for(int i = 0; i < height; ++i){
  strs[i] = new string[width];
 }
 
 foo(strs, width, height);
}

You might want to skip doing manual memory management and possible use std::vectors

typedef std::vector<string> StringArray;

void foo(std::vector<StringArray>& arrayList){
 //...
}

int main(){
  int height = 0, width = 0;
  cin >> height >> width;
  std::vector<StringArray> myList(height , StringArray(width) );
  
   foo(myList);
}

Edited by firstPerson: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.