我一直在尝试执行以下代码。.但是,我一遍又一遍地遇到相同的错误,我不知道为什么!
我的代码:
#include#include #include #include 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; }
我已经计算了我在数组中输入的元素数量,并且它们不超过20个!
另外,为什么我总是收到“隐式转换丢失整数精度错误”的信息?
我猜您想创建一个指针数组而不是字符数组。
尝试这个:
#include#include #include #include 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