Like it says in the title im having problems with putting a 2D array into a function.

Im not sure on the syntax of putting an array into a function and using it, any help is much appreciated!

Thanks in advance!

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void updateGrid(char grid[][]);

int height = 2, width = 2;

int main(int argc, char *argv[])
{
    // CREATES A GRID 2X2 AND POPULATES IT
    char grid[height][width];
    grid[0][0] = 'a';
    grid[0][1] = 'b';
    grid[1][0] = 'c';
    grid[1][1] = 'd';
    for(int i = 0; i < height; i++)
    {
            for(int j = 0; j<width; j++)
            {
                    cout << grid[i][j];
            }
            cout << endl;
    }
    
    // to run function type update
    string str;
    cout << "type update to run function:";
    cin >> str;
    if(str == "update")
    {
           cout << "\n\n\n";
           updateGrid(grid[][]);
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void updateGrid(char grid[][])
{
     for(int i = 0; i < height; i++)
     {
         for(int j = 0; j < width; j++)
         {
            cout << grid[i][j];
         }
         cout << endl;
     }
         
}

Recommended Answers

All 9 Replies

FYI the terminology is "passing a 2d array to a function".

Here are multiple solutions:

http://www.programmersheaven.com/mb/CandCPP/315619/315619/how-to-pass-a-two-dimensional-array-by-ref/

I would opt for the "use a vector of vectors rather than a 2D array" option.

David

Thanks for the link im still having problems getting it to work can anyone provide addition assistance?

The changes to the code are that i tried amongst various others was.. grid[][] is now grid[2][2] except when your passing it into the function which is just grid.

The compiler throws me the error:

34 F:\main.cpp cannot convert `char (*)[((unsigned int)((int)width))]' to `char (*)[2]' for argument `1' to `void updateGrid(char (*)[2])'

Another related question of mine is, why cant the array be declared outside of the functions so that i dont have to pass it..?

doolali, that is called a global variable. They are VERY BAD. They may seem like a nice idea when you only have a short program, but when the code gets bigger global variables make things very difficult to manage.

I no they are but i really cant get passing to work, ive just started c++ from java which was much easier with passing arrays and i see no other choice, and in this situation it is a short program so i dont see any other solution with the failing passing.

Did you look at the link I sent? Like I mentioned, it had several solutions for you.

I have looked at that link, its to pass a 2d array by ref and i dont understand it and what i have understood from it hasnt worked.

What is says is that you cannot pass a variable length 2D array to a function. You have to know the dimensions ahead of time, and hardcode them in the function signature.

This works properly:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void updateGrid(char grid[][2]);

int height = 2, width = 2;

int main(int argc, char *argv[])
{
    // CREATES A GRID 2X2 AND POPULATES IT
    char grid[2][2];
    grid[0][0] = 'a';
    grid[0][1] = 'b';
    grid[1][0] = 'c';
    grid[1][1] = 'd';
    for(int i = 0; i < height; i++)
    {
            for(int j = 0; j<width; j++)
            {
                    cout << grid[i][j];
            }
            cout << endl;
    }

    // to run function type update
    string str;
    cout << "type update to run function:";
    cin >> str;
    if(str == "update")
    {
           cout << "\n\n\n";
           updateGrid(grid);
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

void updateGrid(char grid[][2])
{
     for(int i = 0; i < height; i++)
     {
         for(int j = 0; j < width; j++)
         {
            cout << grid[i][j];
         }
         cout << endl;
     }

}

If you cannot know the size of the grid at compile time, you must use dynamic memory - either in the form of std::vector's or use malloc/new to allocate the array.

char* x = (char *) malloc(num_rows * num_cols * sizeof(char));

I'm not sure if

char* x = new char[num_rows * num_cols];

does the same thing. I'm also not sure if you automatically get the [j] style accessors with the above methods.

Thanks for the help, i completely hard coded my 2d array and it started working, im still unsure on pointers and how they operate fully for using those so theres a lot more reading to be done.
Thanks!!!

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.