1

I am working on a board game and have a 2d char array for board in my main:

char board[*size][*size];

for(int i = 0; i < *size; i++) {
    for(int j = 0; j < *size; j++) {
    board[i][j] = ".";
    }
}

I want to use this in my function named playerOneMove(?), change some of its elements and than bring back to main again to use it in playerTwoMove(?)

I can do this with 1D integer arrays but i couldn't make this work. I just want to learn the method, not full code.

Ayro
  • 42
  • 5
  • Possible duplicate of [2D Array as function parameter](https://stackoverflow.com/questions/46506782/2d-array-as-function-parameter) – boriaz50 Oct 01 '17 at 02:25
  • 1
    [My preferred method](https://stackoverflow.com/a/33425887/2706707). Note that you can also create a template function that will do it. The template does not have to be a pass-along function like in my example; it can be a full-function to be instantiated for every T,N,M of array you pass it. – Dúthomhas Oct 01 '17 at 02:40
  • 1
    This [explanation](https://stackoverflow.com/a/17569578/5306694) was very helpful to me when I was learning 2d arrays. – goosejump Oct 01 '17 at 03:08
  • This doesn’t address the question, but I’m having trouble imagining why `size` would be a pointer and not a value in `main`. – Pete Becker Oct 01 '17 at 03:34

2 Answers2

0

enter image description here

The best way to learn is by looking at code.

The below code passes a 2D array. Study it.

#include <iostream>
#include <cstdio>
using namespace std;


// Returns a pointer to a newly created 2d array the array2D has size [height x width]

int** create2DArray(unsigned height, unsigned width){
  int** array2D = 0;
  array2D = new int*[height];

  for (int h = 0; h < height; h++){
        array2D[h] = new int[width];

        for (int w = 0; w < width; w++){
              // fill in some initial values
              // (filling in zeros would be more logic, but this is just for the example)
              array2D[h][w] = w + width * h;
        }
  }

  return array2D;
}

int main(){

  printf("Creating a 2D array2D\n");
  printf("\n");

  int height = 15;
  int width = 10;

  int** my2DArray = create2DArray(height, width);
  printf("Array sized [%i,%i] created.\n\n", height, width);

  // print contents of the array2D
  printf("Array contents: \n");

  for (int h = 0; h < height; h++)  {
        for (int w = 0; w < width; w++)
        {
              printf("%i,", my2DArray[h][w]);
        }
        printf("\n");
  }

  // important: clean up memory
  printf("\n");
  printf("Cleaning up memory...\n");

  for (  h = 0; h < height; h++){
    delete [] my2DArray[h];
  }

  delete [] my2DArray;
  my2DArray = 0;
  printf("Ready.\n");

  return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • Isn't is this code just showing how to return an array from function? My array is declared and filled in main and i want to use and change the elements of it in a function and then return it. – Ayro Oct 01 '17 at 10:11
  • ok. I understand. But You can easily move the stuff from the function into the main. – Software_Designer Oct 02 '17 at 04:38
0

Here's just math formulas for converting any kind of 2d array (width = height OR width != height) where x, y - indexes of 2d array; index - index of 1d array. That's for base 1 - first 2d element has index 11 (x=1, y=1). Guess you may implement it wherever you wish.

2D to 1D

index = width * (x-1) + y

1D to 2D

x = (index / width) + 1

y = ((index - 1) % width) + 1

For base 0 - 1st element indexes x=0, y=0

2D to 1D

index = width * x + y

1D to 2D

x = index / width

y = (index - 1) % width

Community
  • 1
  • 1
Gleb B
  • 151
  • 2
  • 8