C语言警告:数组初始化器中有过多的元素;在‘xxx’的初始化附近;期望‘char *’,但类型为‘int’。

7

在尝试编译C语言程序时,我收到了一些警告:

13:20: warning: excess elements in array initializer [enabled by default]

13:20: warning: (near initialization for ‘litera’) [enabled by default]

22:18: warning: excess elements in array initializer [enabled by default]

22:18: warning: (near initialization for ‘liczba’) [enabled by default]

43:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

45:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

55:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

源代码如下:

char litera[63] = {'E', 'G', 'H', 'F', 'E', 'G', 'H', 'F',
                   'D', 'B', 'A', 'C', 'D', 'B', 'A', 'C', 
                   'B', 'A', 'C', 'D', 'C', 'A', 'B', 'D', 
                   'F', 'H', 'G', 'E', 'F', 'H', 'G', 'E', 
                   'C', 'D', 'B', 'A', 'B', 'A', 'C', 'D', 
                   'F', 'E', 'G', 'H', 'G', 'H', 'F', 'G', 
                   'H', 'F', 'E', 'F', 'H', 'G', 'E', 'C',
    /*line13:*/    'A', 'B', 'D', 'C', 'A', 'B', 'D', 'E'};

int liczba[63] ={'8', '7', '5', '6', '4', '3', '1', '2', 
                 '1', '2', '4', '3', '5', '6', '8', '7', 
                 '5', '7', '8', '6', '4', '3', '1', '2', 
                 '1', '2', '4', '3', '5', '6', '8', '7', 
                 '6', '8', '7', '5', '3', '1', '2', '4', 
                 '3', '1', '2', '4', '6', '8', '7', '5', 
                 '7', '8', '6', '4', '3', '1', '2', '1', 
    /*line22*/   '2', '4', '3', '5', '6', '8', '7', '5'};


int i=0, x=0, n;
char a;
int b=0;
printf("Podaj literę z pola startowego skoczka(duże litery)\n");
scanf("%s", &a);
printf("Podaj liczbę z pola startowego skoczka \n");
scanf("%d", &b);

if (b<=0 || b>8){
    printf("Zła pozycja skoczka\n");
    return 0;
    }

while (litera[i] != a || liczba[i] != b){
    ++i;
    }
n=i;

/*line43*/ printf("Trasa skoczka od punktu %s %d to:\n", a, b); 
while (i<=64){
/*line45*/ printf("%s%d ", litera[i], liczba[i]);
    ++i;

    ++x;
    x=x%8;
    if(x==0)
        printf("/n");
    }
i=0;
while (i<n){
/*line55*/ printf("%s%d ", litera[i], liczba[i]);

    ++i;

    ++x;
    x=x%8;
    if(x==0)
        printf("/n");
    }

我在scanf("%d", &b);之后遇到了“核心转储”错误,但是int b=0;并没有出现问题...


3
您的初始化程序有8*8 == 64个元素。您的声明指出“litera”的大小为63。您不能在大小为63的数组中存储64个元素。 - melpomene
3个回答

8
这里有两个错误:首先,你试图声明一个包含64个元素的arrays[63]数组来存储它们,很可能是因为你混淆了数组的大小(n)和最大可能的索引值(即 n - 1)。所以它应该是litera[64]liczba[64]。顺便说一句,你还需要改变这一行:while (i<=64):否则你会试图访问第65个元素。

其次,你试图使用%s格式说明符来填充char值,而你应该在这里使用%c

此外,我不禁想知道为什么你声明liczba数组为一个存储int的数组,然后用char数组初始化它。所有这些'1'、'2'等字面量都代表它们的字符编码,而不是相应的数字。我怀疑这不是你的意图。


我在尝试使用scanf(“%d”,&b);后也遇到了“违反内存保护(核心已转储)”的问题。那么我该如何像我在liczba []中尝试的那样存储数字的顺序呢? - Naan
1
你可以使用数字字面量设置一个数组初始化器,例如 {8, 7, 5, ... } - 没有引号包围数字。 - raina77ow

0

char litera[63] -> char litera[64] int liczba[63] -> int liczba[64]

在数组大小初始化的位置上使用64。当您初始化数组大小时,多初始化一个索引。


0

索引始终从 i=0 开始,但元素数量计数从 1 开始。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接