简体   繁体   中英

How to define a function, parameters of which take variables from main() function?

I encountered a need to define a function, parameters of which take arguments from main() function. Here's the code:

#include <iostream>
using namespace std;

void unravel(char arr[][column], int field[][column], int x, int y) { //revealing adjoined cells
    for (int minusrows = -1; minusrows < 2; minusrows++){
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
            arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
        }
    }
}

int main (){
    
    int row, column;
    cin >> row >> column;

    char a[row][column];
    int field[row][column];
    
    for (int i = 0; i < row; i++){ //filling a with asterisks, field with integers
        for (int j = 0; j < column; j++){
            a[i][j] = '*';
            field[i][j] = i + j;
        }
    }
 
    int x = 2, y = 3;
    
    unravel(a, field, x, y);
    
    for (int i = 0; i < 5; i++){ //printing out the array a
        for (int j = 0; j < 5; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
  
    return 0;
}

And here are the errors that I get:

main.cpp:4:25: error: ‘column’ was not declared in this scope
 void unravel(char arr[][column], int field[][column], int x, int y) {
                         ^~~~~~
main.cpp:4:32: error: expected ‘)’ before ‘,’ token
 void unravel(char arr[][column], int field[][column], int x, int y) {
                                ^
main.cpp:4:34: error: expected unqualified-id before ‘int’
 void unravel(char arr[][column], int field[][column], int x, int y) {
                                  ^~~

So the goal is to change the values around a cell in a 2D array a[ ][ ] that initially consists of asterisks (*) to the values of an int 2D array field[ ][ ] of the same indices. For that is responsible the function unravel() . The code worked perfectly when I had integer 5 instead of row and column .

#include <iostream>
using namespace std;

void unravel(char arr[][5], int field[][5], int x, int y) { //revealing adjoined cells
    for (int minusrows = -1; minusrows < 2; minusrows++){
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
            arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
        }
    }
}

int main (){
    
    char a[5][5];
    int field[5][5];
    
    for (int i = 0; i < 5; i++){ //filling a with asterisks, field with integers
        for (int j = 0; j < 5; j++){
            a[i][j] = '*';
            field[i][j] = i + j;
        }
    }
 
    int x = 2, y = 3;
    
    unravel(a, field, x, y);
    
    for (int i = 0; i < 5; i++){ //printing out the array a
        for (int j = 0; j < 5; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
  
    return 0;
}

Therefore the question: is it possible to have some data stored from the main() function so that the function unravel() would work as I expect it to? I know that variables are defined within the scope. Anything else I saw about the errors I got didn't have much related to my problem. Sorry if the post seems long. Thanks in advance.

These lines in main() :

int row, column;
cin >> row >> column;

char a[row][column];
int field[row][column];

results in attempting to declare an array using runtime values. This is not valid C++ .

Arrays in C++ must have their sizes denoted by a compile-time expression, not a runtime value.

The simplest solution is to use std::vector . Using typedef or using simplifies this:

#include <vector>

using Char1D = std::vector<char>;
using Char2D = std::vector<Char1D>;
using Int1D = std::vector<int>;
using Int2D = std::vector<Int1D>;

int main()
{
    int row, column;
    cin >> row >> column;

    Char2D a(row, Char1D(column,'*'));
    Int2D field(row, Int1D(column));

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
            field[i][j] = i + j;
    }
    //...
    int x = 2, y = 3;

    unravel(a, field, x, y);
}

Then the unravel function, when using these definitions, also becomes easier to deal with:

void unravel(Char2D& arr, Int2D& field, int x, int y)
{ 
    //... 
}

Note that we pass references, mimicking the behavior if you actually used 2D arrays.

Basically everything else stays the same.

Dynamic 2D array can be declared as a array of pointers to array like this

int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
    a[i] = new int[colCount];

for more details please refer: How do I declare a 2d array in C++ using new?

So your code will work fine if you use new keyword to declare 2D array.

#include <iostream>
using namespace std;

void unravel(char **arr, int **field, int x, int y) { //revealing adjoined cells
    for (int minusrows = -1; minusrows < 2; minusrows++){
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
            arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
        }
    }
}

int main (){
    
    int row, column;
    cin >> row >> column;

    char **a = new char*[row];
    int **field = new int*[row];
    
    for (int i = 0; i < row; i++){ //filling a with asterisks, field with integers
        a[i]=new char[column];
        field[i] = new int[column];
        for (int j = 0; j < column; j++){
            a[i][j] = '*';
            field[i][j] = i + j;
        }
    }
 
    int x = 2, y = 3;
    
    unravel(a, field, x, y);
    
    for (int i = 0; i < row; i++){ //printing out the array a
        for (int j = 0; j < column; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    
    for(int i=0; i<row; i++){
        delete [] a[i];
        delete [] field[i];
    }
    
    delete [] a;
    delete [] field;
    
    return 0;
}

Please note: memory is allocated on heap using new keyword. In order to prevent memory leak we should free allocated memory at the end. It can be done like this in this program.

for(int i=0; i<row; i++){
    delete [] a[i];
    delete [] field[i];
}

delete [] a;
delete [] field;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM