我试图将字符串数组的位置作为int数组的参数传递,并且我得到了无效的间接符作为错误。我想计算我随机生成并抓到的小精灵的频率。我做了一些相当多的研究,但仍然无法理解它的概念。附:我的讲师正在教C的过时版本,以便介绍printf(s)。将字符数组转换为值并将其放入C中的int数组中
int encounter() {
//list of pokemons
char pokemon[5][10] = {"Magikarp", "Zubat", "Eevee", "Magmar", "Pikachu"};
int type[5] = {0,0,0,0,0};
int i;
int a=0;
//random number generator from 0 - 4
srand(time(NULL));
i = (rand() % 5);
//prints out the pokemon name
printf("A wild %s appeared! \n", pokemon[i]);
type[pokemon[i]] += 1;
for(a=0; a<6; a++) {
printf(type[a]);
}
return 0;
}
“***我的讲师教的C过时的版本,所以介意的printf(S)。***” 是什么意思呢? –
您使用的C版本不能过时:您正在使用内联注释,直到C99时才会使用内置注释。 – yellowantphil
间接问题在这里是'type [pokemon [i]] + = 1;'。用简单的'type [i] + = 1;'替换那个代码,因为pokemon [i]是一个char数组。 –