This is my first programming in C. In printGrid() function, I tried to assign some values to the 2D array, and the rest spots in the 2D array just assign 0. I test it as soon as the value is assigned to an array and it works as expected. However, when I use display() function to print out the whole array, it did not work correctly, all the values in the array are 0. I have spent several hours to do the search and debugged it by myself before I post a question here. Could someone help me with this question? Thanks.
#include <stdio.h>
#include <stdlib.h>
const int max_height = 4;
int height; //the height of pile
int center_x_index, center_y_index; //index of input parameter that is center of the plane
int unstable;
int drop_status;
int *grid[5][5];
int is_stable(int total_param, char *piles[]);
void display(int *grid[5][5]);
/*
Function to print out the 23*23 grid with given number of piles
*/
void printGrid(int total_param, char *piles[])
{
int i, j, k, size, assigned;
size = 5;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
for (k = 1; k < total_param; k = k + 3)
{
if ((atoi(piles[k]) == i) && (atoi(piles[k + 1]) == j))
{
height = atoi(piles[k + 2]);
//find the central pile of the plane and drop a grain of sand onto this pile.
if ((i == j) && i == (size / 2))
{
center_x_index = k;
center_y_index = k + 1;
if (drop_status > 0 && is_stable(total_param, piles))
{
drop();
}
}
grid[i][j] = &height; //store value into 2D array
//printf("2D array: %d", *grid[i][j]);
printf("%d", height);
assigned = 1;
break;
}
else
{
assigned = 0;
}
}
if (assigned != 1)
{
height = 0;
grid[i][j] = &height;
printf("%d", 0);
}
}
printf("\n");
}
}
void display(int *grid[5][5])
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d", *grid[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
printGrid(argc, argv);
printf("\n");
}