Return 2d array from C++

4.1k views Asked by At

Inside a function, I make a 2d array that fills itself from a text file and needs to get returned to main. The array stays a constant size through the whole program.

I know this is something that gets asked a lot, but I always seem to get one of two answers:

Use std::vector or std::array or some other STD function. I don't really understand how these work, is there any site actually explaining them and how they act compared to normal arrays? Are there any special #includes that I need?

Or

Use a pointer to the array, and return the pointer. First, on some of the answers to this it apparently doesn't work because of local arrays. How do I tell when it does and doesn't work? How do I use this array back in the main function?

I'm having more trouble with the concept of pointers and std::things than with the actual code, so if there's a website you know explains it particularly well, feel free to just put that.

3

There are 3 answers

1
Lawrence H On

Sounds like you are new to C++. If this is indeed the case, I would suggest using arrays for now because you probably won't be using any of the stuff that STL containers give you. Now, let's talk about pointers.

You are correct that if you declare a local array in your function, the main function won't have access to it. However, this is not the case if you dynamically allocate the array using the new keyword. When you use new to allocate your array, you essentially tell the compiler to reserve a chunk of memory for your program. You can then access it using a pointer, which is really just the address of that chunk of memory you reserved. Therefore, instead of passing the entire array to the main function, all you need to do is pass a pointer (address) to that array.

Here are some relevant explanations. I will add to them as I find more:

0
ash On

The easiest way to create a 2d array is as follows:

char (*array)[10];
array = new array[5][10];

Two dimensional arrays can be tricky to declare. The parenthesis above in the variable declaration are important to tell the compiler array is a pointer to an array of 10 characters.

It is really essential to understand pointers with C and C++ unless using the std:: collections. Even then, pointers are widely prevalent, and incorrect use can be devastating to a program.

6
Neil Kirk On

Not necessarily the best solution, but the easiest way to get it working with vectors. The advantages are that you don't need to delete memory (happens automatically) and the array is bounds-checked in debug mode on most compilers.

#include <vector>
#include <iostream>

using array2D = std::vector< std::vector< int > >;

array2D MyFunc(int x_size, int y_size)
{
    array2D array(y_size, vector< int >(x_size));

    int i = 0;
    for (int y = 0; y < array.size(); y++)
    {
        for (int x = 0; x < array[y].size(); x++)
        {
            // note the order of the index
            array[y][x] = i++;
        }
    }

    return array;
}

int main()
{
    array2D bob = MyFunc(10, 5);
    for (int y = 0; y < bob.size(); y++)
    {
        for (int x = 0; x < bob[y].size(); x++)
        {
            cout << bob[y][x] << "\n";
        }
    }
}

Live example: http://ideone.com/K4ilfX