typedef struct grid Grid;

struct grid{
    char grid[MAX_ROW][MAX_COL];
}
void storeGrid(Grid *store_grid,int row, int col){
    Grid *store_grid;
	store_grid = (Grid)malloc(sizeof(Grid));
	
	//Grid storeGrid;

    //int grid[MAX_ROW][MAX_COL];
 
    int rowNum, colNum;
    for(rowNum = 0; rowNum <= row; rowNum++){
        for(colNum = 0;colNum < col; colNum++){
            store_grid->grid[rowNum - 1][colNum] = 'a'; 
        }
    }
    // Calls the PrintGrid function, which prints out the grid
    //printGrid(storeGrid.grid,row,col); 
    
    
}

I edited this code in order to change values in the 2d array directly but this doesnt seem to be working. So i was wondering if any1 could make suggestions so that i can directly edit the 2d array in the struct rather then copies being made in the function and then dissappearing after the function has ended.

Thanks.

These are functionally equivalent, if you want to get a grid out of storegrid back to the caller.

typedef struct grid Grid;

struct grid{
    char grid[MAX_ROW][MAX_COL];
}

void storeGrid1(Grid **result,int row, int col){
  Grid *mygrid;
  mygrid = malloc(sizeof(Grid));
  // more stuff
  *result = mygrid;
}

Grid *storeGrid2(int row, int col){
  Grid *mygrid;
  mygrid = malloc(sizeof(Grid));
  // more stuff
  return mygrid;
}

int main ( ) {
  Grid  *g1, *g2;
  storeGrid1( &g1, 3, 3 );
  g2 = storeGrid2( 3, 3 );
  free( g1 );
  free( g2 );
  return 0;
}

> store_grid->grid[rowNum - 1][colNum] = 'a';
This is an out of bound reference for the first row.

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.