This is part of my Wumpus Hunt program. Suggestions were to clean up the main() part of the program by creating functions. I am working on that and ran into this difficulty (for me). The functions that relate to the rooms of the game need to return an array. I searched this topic and found that it is NOT AT ALL a clear and simple affair to have a function return an array in C++.

The clearest explanation I have seen so far is to create an array in heap memory and then have the function return a pointer to the first element of that array. A person said that a stack memory array can't be used because it will be destroyed when it goes out of scope at the end of the function.

This pointer business makes sense because this exercise is in the chapter on pointers, heap memory, arrays/vectors, and constructors/destructors.

Here is what was given as the correct way to return an array. Is that so?

int foo(int n){
    int* ptr = new int[n];
    for (int i = 0; i < n; i++) {
        ptr[i] = 2*i;
    }
    return ptr;
}

If that is correct, my next question is will it also work for a 2 dimensional array? I am using 2D arrays in my code. Is the first element of a 2D array [0][0]? Will a pointer point to ptr[0][0] in the following example?

int foo(int n, int m){
    int* ptr = new int[n][m];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ptr[i][j] = 2*i + 4*j;
        }   
    }
    return ptr;
}

Thanks in advance.

This is part of my Wumpus Hunt program. Suggestions were to clean up the main() part of the program by creating functions. I am working on that and ran into this difficulty (for me). The functions that relate to the rooms of the game need to return an array. I searched this topic and found that it is NOT AT ALL a clear and simple affair to have a function return an array in C++.

The clearest explanation I have seen so far is to create an array in heap memory and then have the function return a pointer to the first element of that array. A person said that a stack memory array can't be used because it will be destroyed when it goes out of scope at the end of the function.

This pointer business makes sense because this exercise is in the chapter on pointers, heap memory, arrays/vectors, and constructors/destructors.

Here is what was given as the correct way to return an array. Is that so?

int foo(int n){
    int* ptr = new int[n];
    for (int i = 0; i < n; i++) {
        ptr[i] = 2*i;
    }
    return ptr;
}

If that is correct, my next question is will it also work for a 2 dimensional array? I am using 2D arrays in my code. Is the first element of a 2D array [0][0]? Will a pointer point to ptr[0][0] in the following example?

int foo(int n, int m){
    int* ptr = new int[n][m];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ptr[i][j] = 2*i + 4*j;
        }   
    }
    return ptr;
}

Thanks in advance.

Hello, concerning your first example you are not totally right. You have to return a pointer and not an integer value. Something like this:

int* foo() {
  ...
}

Concerning your 2d array example you're not right. You need to create a pointer to a pointer for your 2d array and the pointer that your will return, will point to the first element of your array which is arr[0][0]. Try this..

#include <iostream>

using namespace std;

typedef int* IntP;

IntP* create2dArr(int &l, int &c);

int main()
{
   int lines, columns;
  IntP *tab;
  tab = create2dArr(lines, columns); //at this point the function returns the int **p pointer to tab
                                   //from now on tab will point to our desired array...
  cout << "the content of the returning 2d array is:\n";
  for (int i = 0; i < lines; i++) {
   for (int j = 0; j < columns; j++)
    cout << tab[i][j] << "\t";
     cout << endl;
  }
   return 0;
}

 IntP* create2dArr(int &l, int &c) {
     int lines, columns;
    cout << "give me number of lines: ";
    cin >> lines;
    cout << "give me number of columns: ";
    cin >> columns;
    l = lines;   //take these values in main by reference
    c = columns;
     IntP *p = new IntP[lines];
     for (int i = 0; i < lines; i++)
      p[i] = new int[columns];

     cout << "give me " << lines << "x" << columns << " numbers to fill the array\n";
     for (int i = 0; i < lines; i++)
      for (int j = 0; j < columns; j++)
       cin >> p[i][j];

     return p;  //the returning pointer that points to a 2darray
 }

Edited 6 Years Ago by vanalex: n/a

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