2D阵列中的scanf错误

时间:2016-04-10 18:05:35

标签: c scanf

我有以下代码:

#include <stdio.h>

int main() {

int tc,T;
scanf("%d", &T);

for(tc=0; tc<T; tc++){
    int flag=0;
    int R,C;

    scanf("%d %d", &R, &C);
    {
        int i,j;
        int r,c;
        int Grid[R][C];

        //Read the Grid
        for(i=0; i<R; i++){
            for(j=0; j<C; j++){
                scanf("%d", &Grid[i,j]);
            }
        }

        scanf("%d %d", &r, &c);
        {
            int Pattern[r][c];

            //Read the Grid
            for(i=0; i<r; i++){
                for(j=0; j<c; j++){
                    scanf("%d", &Pattern[i,j]);
                }
            }
            //Here we have both the Grid and the Pattern
            for(i=0; i<R; i++){
                for(j=0; j<C; j++){
                    if(Grid[i,j]==Pattern[0,0] && ((i+r)<=R) && ((j+c)<=C)){
                        int x,y;
                        int innerFlag=1;

                        for(x=0; x<r; x++){
                            for(y=0; y<c; y++){
                                if(Grid[x+i,y+j]!=Pattern[x,y]) innerFlag=0;
                            }
                        }

                        if(innerFlag == 1){ 
                            //Set the flag to 1 and break out of the nested loops
                            flag=1;
                            j=C;
                            i=R;
                        }
                    }
                }
            }

            //Here all the calculation is done and the flag is set
            if(flag==1) printf("YES\n");
            else printf("NO\n");
        }

    }
}

return 0;
}

这给了我以下错误:

  1. 警告:格式'%d'需要类型'int '的参数,但参数2的类型为'int()[(sizetype)(C)]' scanf(“%d”,&amp; Grid [i,j]);
  2. 警告:格式'%d'需要类型'int '的参数,但参数2的类型为'int()[(sizetype)(c)]' scanf(“%d”,&amp; Pattern [i,j]);
  3. 你知道这段代码有什么问题吗?

    P.S我还检查了网格和模式,它读取垃圾!

3 个答案:

答案 0 :(得分:3)

在C中你应该下标这样的数组:array[i][j] array[i, j]相当于array[j],因为i会被忽略。

答案 1 :(得分:1)

c中的数组不像Grid[i, j]那样被访问,而是被Grid[i][j]访问。所以将其更改为代码中的内容。同样,Pattern[i, j]应写为Pattern[i][j]

这将解决您的问题。

乐意帮助! :)

答案 2 :(得分:0)

尝试以下内容:

#include <stdio.h>

int main() {

int tc,T;
printf("Enter one number\n");
scanf("%d", &T);

for(tc=0; tc<T; tc++){
int flag=0;
int R,C;
printf("Enter 2 numbers\n");
scanf("%d %d", &R, &C);
{
    int i,j;
    int r,c;
    int Grid[R][C];

    printf("now reading the grid\n");
    //Read the Grid
    for(i=0; i<R; i++){
        for(j=0; j<C; j++){
            scanf("%d", &Grid[i][j]);
        }
    }

    printf("Enter 2 mre numbers\n");
    scanf("%d %d", &r, &c);
    {
        int Pattern[r][c];
        printf("now reading the pattern\n");

        //Read the Grid
        for(i=0; i<r; i++){
            for(j=0; j<c; j++){
                scanf("%d", &Pattern[i][j]);
            }
        }
        //Here we have both the Grid and the Pattern
        for(i=0; i<R; i++){
            for(j=0; j<C; j++){
                if(Grid[i][j]==Pattern[0][0] && ((i+r)<=R) && ((j+c)<=C)){
                    int x,y;
                    int innerFlag=1;

                    for(x=0; x<r; x++){
                        for(y=0; y<c; y++){
                            if(Grid[x+i][y+j]!=Pattern[x][y]) innerFlag=0;
                        }
                    }

                    if(innerFlag == 1){ 
                        //Set the flag to 1 and break out of the nested loops
                        flag=1;
                        j=C;
                        i=R;
                    }
                }
            }
        }

        //Here all the calculation is done and the flag is set
        if(flag==1) printf("YES\n");
        else printf("NO\n");
    }

}
}

return 0;
}