This version runs well on old compilers:
Code:
#include <stdio.h>
#include <stdlib.h>
int max_rows;
int max_columns;
//we can pass this way without knowing the values of max rows and columns
//we could change it, if we knew the sizes at this point
void print(char**);
int main(int argc, char *argv[]){
char **grid;
int i, j;
max_rows = 3;
max_columns = 3;
*grid = malloc(max_rows * sizeof(char*));
for(i = 0; i < max_rows; i++)
grid = malloc(max_columns * sizeof(char)); //not a pointer size
for(i = 0; i < max_rows; i++){
for(j = 0; j < max_columns; j++){
grid[i][j] = 'X';
}
}
print(grid);
//free each row
for(i = 0; i < max_rows; i++)
free(grid[i]);
//then free the whole array
free(grid);
printf("\n\n\t\t\t press enter when ready ");
i = getchar();
return 0;
}
void print(char **grid) {
int i, j;
for(i = 0; i < max_rows; i++){
for(j = 0; j < max_columns; j++){
printf("%c ", grid[i][j]);
}
printf("\n");
}
}