之前我問這個問題我已經調試了幾個小時,並閱讀類似的問題,但我仍然無法解決這個問題。我檢查了當我定義void drop(int size,int x_coor,int y_coor,int grid)這個方法時發生的錯誤。如何解決這個錯誤?錯誤:','預計(得到「10」)在C編程
這是錯誤:
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;
}
請勿使用宏。 – o11c
@ o11c不幸的是,你在C中沒有太多的選擇。你不能聲明一個常量來聲明數組大小 –
@ user7027796,這就是爲什麼在大多數約定中用CAPS編寫常量的原因。如果你寫'SIZE',問題就不會出現。無論如何避免這樣的通用詞,並使用像'GRID_SIZE'這樣的描述性名稱。 [不要使用'atoi'](https://stackoverflow.com/q/17710018/995714) –