0

before I ask this question I have already debugged it for hours and read similar question post but I still cannot solve this problem. I checked that the error occurs when I define void drop(int size, int x_coor, int y_coor, int grid) this method.How to fix this error?

This is the error:

main.c:11: error: ',' expected (got "10")

#include <stdio.h>
#include <stdlib.h>

const int max_height = 4;

int height;

#define size 10
int grid[size][size];

void drop(int size, int x_coor, int y_coor, int grid);
int is_stable(int grid[size][size]);
void display(int grid[size][size]);

/*
Function to print out the 23*23 grid with given number of piles
*/

void create_InitialGrid(int total_param, char *piles[])
{
    int i, j, k, assigned;

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

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

                    assigned = 1;
                    break;
                }
                else
                {
                    assigned = 0;
                }

            }
            if (assigned != 1)
            {
                height = 0;
                grid[i][j] = height;
            }

        }
    }

}


//Simulate a grain of sand is dropped onto the center pile of the pane

void drop(int size, int x_coor, int y_coor, int grid)
{
    .........
}

/*
Display the 2D array
*/
void display(int grid[size][size])
{
    int i, j;
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d", grid[i][j]);
        }
        printf("\n");
    }
}


int main(int argc, char *argv[])
{
    create_InitialGrid(argc, argv);

    printf("Initial Grid: \n");

    display(grid);

    printf("////////////////////// \n");
    drop();

    display(grid);


    return 0;
}
CinCout
  • 9,486
  • 12
  • 49
  • 67
  • 2
    Don't use macros. – o11c Aug 30 '17 at 03:37
  • @o11c unfortunately you don't have much choices in C. You can't declare a constant for declaring array sizes – phuclv Aug 30 '17 at 03:47
  • 2
    @user7027796 that's why in most conventions you write constants in CAPS. The problem wouldn't appear if you write `SIZE`. Anyway avoid generic words like that and use descriptive names like `GRID_SIZE`. And [don't use `atoi`](https://stackoverflow.com/q/17710018/995714) – phuclv Aug 30 '17 at 03:52
  • @LưuVĩnhPhúc There are only 3 numbers: 0, 1, and lots. – o11c Aug 30 '17 at 03:53
  • @o11c I don't get you. Try `const int SIZE = 10; int grid[SIZE][SIZE]` – phuclv Aug 30 '17 at 03:57
  • @LưuVĩnhPhúc This is a very good suggestion. – user7027796 Aug 30 '17 at 04:01

2 Answers2

3
void drop(int size, int x_coor, int y_coor, int grid);

It is getting converted to

void drop(int 10, int x_coor, int y_coor, int grid);

After compiler preprocessing step, as you have defined #define size 10

You should declare it as

void drop(int , int , int , int );

Or just use different name for that variable in function declaration

Pras
  • 4,047
  • 10
  • 20
  • Thanks for your help. I changed the naming, now it fixed, but the there is another runtime error: pointer expected. – user7027796 Aug 30 '17 at 03:45
1

I've just tried compiling your code, and it seems like the #define size macro is conflicting with the int size parameter in the drop function.

Here is the output of gcc on my Mac:

temp.c:11:15: error: expected ')'
void drop(int size, int x_coor, int y_coor, int grid);
              ^
temp.c:8:14: note: expanded from macro 'size'
#define size 10
             ^
temp.c:11:10: note: to match this '('
void drop(int size, int x_coor, int y_coor, int grid);
              ^

Hopefully that fixed it! :)