簡體   English   中英

C:警告:數組初始值設定項中的元素過多; 'xxx' 即將初始化; 需要“char *”,但類型為“int”

[英]C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’

我在嘗試用 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);之后我也有“核心轉儲” scanf("%d", &b); , 但int b=0; 不會有問題...

這里有兩個錯誤:首先,您試圖聲明arrays[63]來存儲 64 個元素,因為您可能將數組 ( n ) 的大小與最大可能的索引值(即n - 1 )混淆了。 所以它絕對應該是litera[64]liczba[64] 順便說一句,您也必須更改此行 - while (i<=64) :否則您最終會嘗試訪問第 65 個元素。

其次,您試圖用%s格式說明符為 scanf 填充char值,而您應該在這里使用%c

此外,不禁想知道為什么將liczba數組聲明為存儲int的數組,並使用char數組對其進行初始化。 所有這些“1”、“2”等……文字代表的不是相應的數字——而是它們的字符代碼。 我懷疑那是你的意圖。

字符字面量[63] -> 字符字面量[64] int liczba[63] -> int liczba[64]

用 64 代替數組大小初始化。 初始化數組大小時再初始化 1 個索引。

索引總是從 i = 0 開始,但元素計數從 1 開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM