user7027796
user7027796

Reputation: 47

Why 2D array always print out 0 in C?

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");

}

Upvotes: 0

Views: 675

Answers (2)

Jeff
Jeff

Reputation: 1264

You'll probably want to study up on pointers in C. See this for example.

This line

int *grid[5][5];

declares a 2D array of pointers to integers.

This line

grid[i][j] = &height; //store value into 2D array

stores the address of the variable height to that grid element. At that point in time height is the value you expect, but on the next iteration of the loop you will overwrite it.

So at the end you will have every element in grid pointing to the same variable height. This line

printf("%d", *grid[i][j]);

dereferences the pointer at grid[i][j], that is, it follows the pointer to the variable height and uses its value. Since at this point in time height is zero and every grid element points to it, this will print out all zeros.

What you need to do is instead declare grid as a 2D array of int:

int grid[5][5];

assign the value of height to it instead of its address:

grid[i][j] = height; //store value into 2D array

and use its value directly instead of dereferencing it:

printf("%d", grid[i][j]);

There are probably some other pieces of code you'll have to update as well after changing the definition of grid.

Upvotes: 2

Charles Srstka
Charles Srstka

Reputation: 17060

This line declares a two-dimensional array of int pointers:

int *grid[5][5];

I think you just wanted a two-dimensional array of integers:

int grid[5][5];

The problem is that when you assign the pointer &height to one of these pointers, it assigns the same thing to every one of them; the address of your height variable. When you change height after assigning it to the array, you will change the value that will appear in every single slot in the array, since they're all referencing the original height variable. If you use a two-dimensional array of integers instead, and just assign height, then the value will be copied and you will avoid this problem.

Upvotes: 1

Related Questions