简体   繁体   English

char数组初始值设定项错误中的多余元素

[英]Excess elements in char array initializer error

I've been trying to execute the following code.. However, I keep getting the same errors over and over again and I don't know why! 我一直在尝试执行以下代码..但是,我一遍又一遍地得到同样的错误,我不知道为什么!

My code: 我的代码:

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

int main(void){

    int randomNum;
    char takenWords[4];
    char words[20]={"DOG", "CAT", "ELEPHANT", "CROCODILE", "HIPPOPOTAMUS", "TORTOISE", "TIGER", "FISH", "SEAGULL", "SEAL", "MONKEY", "KANGAROO", "ZEBRA", "GIRAFFE", "RABBIT", "HORSE", "PENGUIN", "BEAR", "SQUIRREL", "HAMSTER"};


    srand(time(NULL));

    for(int i=0; i<4; i++){
        do{
            randomNum = (rand()%20);
        takenWords[i]=words[randomNum];
        }while((strcmp(&words[randomNum], takenWords) == 0)&&((strcmp(&words[randomNum], &takenWords[i])==0)));
        printf("%s\n", &words[randomNum]);
    }
    getchar();
    return 0;
}

I have counted the number of elements which I entered in the array and they do not exceed 20! 我已经计算了我在数组中输入的元素数量,它们不超过20!

Also, why do I keep getting the 'Implicit conversion loses integer precision error'? 另外,为什么我一直得到'隐式转换失去整数精度错误'?

I guess you want to make arrays of pointers instead of arrays of characters. 我想你想要制作指针数组而不是字符数组。

Try this: 尝试这个:

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

int main(void){

    int randomNum;
    const char *takenWords[4];
    const char *words[20]={"DOG", "CAT", "ELEPHANT", "CROCODILE", "HIPPOPOTAMUS", "TORTOISE", "TIGER", "FISH", "SEAGULL", "SEAL", "MONKEY", "KANGAROO", "ZEBRA", "GIRAFFE", "RABBIT", "HORSE", "PENGUIN", "BEAR", "SQUIRREL", "HAMSTER"};


    srand(time(NULL));

    for(int i=0; i<4; i++){
        int dupe=0;
        do{
            randomNum = (rand()%20);
            takenWords[i]=words[randomNum];
            dupe=0;
            for(int j=0;j<i;j++){
                if(strcmp(words[randomNum],takenWords[j])==0)dupe=1;
            }
        }while(dupe);
        printf("%s\n", words[randomNum]);
    }
    getchar();
    return 0;
}
char words[20]={"DOG", "CAT", "ELEPHANT", "CROCODILE", "HIPPOPOTAMUS", "TORTOISE", "TIGER", "FISH", "SEAGULL", "SEAL", "MONKEY", "KANGAROO", "ZEBRA", "GIRAFFE", "RABBIT", "HORSE", "PENGUIN", "BEAR", "SQUIRREL", "HAMSTER"};

Look at the array you have declared closely. 查看您已仔细声明的数组。 what does it contains? 它包含什么?

String literals (ie "DOG" and other string literals). 字符串文字(即"DOG"和其他字符串文字)。

Look at the array itself, it is declared to store char 查看数组本身,声明它存储char

char words[20]

Thats the error. 那是错误。


To store string literals, you need pointer to char , that is char * 要存储字符串文字,需要pointer to char ,即char *

Since you have an array of string literals, you need an array of char * 由于你有一个字符串文字数组,你需要一个char *数组

char* words[20]={"DOG", "CAT", "ELEPHANT", "CROCODILE", "HIPPOPOTAMUS", "TORTOISE", "TIGER", "FISH", "SEAGULL", "SEAL", "MONKEY", "KANGAROO", "ZEBRA", "GIRAFFE", "RABBIT", "HORSE", "PENGUIN", "BEAR", "SQUIRREL", "HAMSTER"};

Since you are using the other array also to point to string literals, declare them the same way 由于您还使用另一个数组指向字符串文字,因此以相同的方式声明它们

char* takenWords[4];

Here in your code char words[20] is an array to hold a single string/word of 20 characters but not 20 different strings. 在你的代码中, char words[20]是一个数组,用于保存20个字符但不包含20个不同字符串的单个字符串/单词。

However in order to declare 20 different strings use a 2D array this way char words[20][20] and then continue to declare the strings/words of your choice of maximum 20 length. 然而,为了声明20个不同的字符串,使用2D数组,这样char字[20] [20]然后继续声明你选择的字符串/单词最多20个长度。

 char words[20][20]={"DOG", "CAT", "ELEPHANT", "CROCODILE", "HIPPOPOTAMUS", "TORTOISE", "TIGER", "FISH", "SEAGULL", "SEAL", "MONKEY", "KANGAROO", "ZEBRA", "GIRAFFE", "RABBIT", "HORSE", "PENGUIN", "BEAR", "SQUIRREL", "HAMSTER"};

Note: char words[20][20] represents an array to hold 20 strings of maximum length 20 characters... 注意: char words[20][20]表示一个数组,用于容纳20个最大长度为20个字符的字符串......

generally char words[m][n] where m,n are integers holds m strings of max length (n-1 chars)+(terminating null character) . 通常char words[m][n]其中m,n是整数,包含m个最大长度的字符串(n-1个字符)+(终止空字符)

char arr [NUMBER_OF_STRING] [MAX_STRING_SIZE]声明它的最大字符串大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:char数组初始化程序中的多余元素 - error: excess elements in char array initializer char数组初始化程序中的多余元素 - excess elements in char array initializer 数组初始化程序中的多余元素 - Excess elements in array initializer 遇到错误:打印出字符串数组期间 char 数组初始值设定项中的元素过多 - Faced an error: excess elements in char array initializer during print out of array of strings 警告:数组初始值设定项中的多余元素 - warning: excess elements in array initializer 不知道如何处理错误“数组初始值设定项中的元素过多” - Not sure how to deal with the error "excess elements in array initializer" 通过指向 2D 字符数组的指针访问。 警告:数组初始值设定项中的多余元素 - access via pointer to 2D char array. warning: excess elements in array initializer C:警告:数组初始值设定项中的元素过多; &#39;xxx&#39; 即将初始化; 需要“char *”,但类型为“int” - C: warning: excess elements in array initializer; near initialization for ‘xxx' ; expects ‘char *’, but has type ‘int’ 编译警告。数组初始化程序中的多余元素 - Compilation Warning.Excess elements in array initializer 在数组初始值设定项中有多余的元素可以吗? - Is it ok to have excess elements in array initializer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM