Excess elements in char array initializer error

本小妞迷上赌 提交于 2020-05-13 07:28:07

问题


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!

Also, why do I keep getting the 'Implicit conversion loses integer precision error'?


回答1:


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;
}



回答2:


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 (i.e. "DOG" and other string literals).

Look at the array itself, it is declared to store char

char words[20]

Thats the error.


To store string literals, you need pointer to char, that is char *

Since you have an array of string literals, you need an array of 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];



回答3:


Here in your code char words[20] is an array to hold a single string/word of 20 characters but not 20 different strings.

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.

 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...

generally char words[m][n] where m,n are integers holds m strings of max length (n-1 chars)+(terminating null character).




回答4:


char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] declare maximum string size that's it



来源:https://stackoverflow.com/questions/34024433/excess-elements-in-char-array-initializer-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!