0

I want to make a function which takes an existing 9x9 empty array of integers, and inserts values taken from a file (so the function also gets the file name as input). But I cant really figure out how to do this properly, even though I've tried quite a few different methods. Basically, here is what I do:

int board = new int[9][9] //The empty array

int addToArray(int *board, string filename) {

    /* Code to insert values in each field is tested and works so 
       I will just show a quick example. The below is NOT the full code */
    int someValue = 5;
    board[0][0]   = someValue;

    /* Return a value depending on whether or not it succeeds. The variable 'succes'
       is defined in the full code */
    if (succes) {
        return 0;
    } else {
        return -1;
    }
}

This is a very reduced example, compared to the actual code, but it is overall function of passing a pointer to a an array into some function, and have that function modifying the array, that I want. Can anyone tell me how to do this?

frank koch
  • 1,048
  • 2
  • 13
  • 25
Jakob Busk Sørensen
  • 4,592
  • 4
  • 32
  • 64

2 Answers2

0

In case anyone ever ends reading the question, I used method number 3 from Johnny's link. I copy pasted the code and added some comments, for convenience...

// Make an empty, and un-sized, array
int **array;

// Make the arrays 9 long
array = new int *[9];

// For each of the 9 arrays, make a new array of length 9, resulting in a 9x9 array.
for(int i = 0; i < 9; i++)
    array[i] = new int[9];

// The function to modify the array
void addToArray(int **a)
{
    // Function code. Note that ** is not used when calling array[][] in here.
}
passFunc(array); 
Jakob Busk Sørensen
  • 4,592
  • 4
  • 32
  • 64
0

It should be simply

int **board

in the function arguments.

The simple rule is that *name = name[ ], so as many [ ] you have in the array, you need to have as many * in the parameters.

Midas
  • 1