gpt4 book ai didi

c - 在函数内部使用指针时出错

转载 作者:行者123 更新时间:2023-11-30 19:07:07 25 4
gpt4 key购买 nike

我有一个 void 函数,它工作得很好,直到我尝试使用它来显示字符串。

void Frandom(int tam, char* itens[]){

int pote[tam];

srand((time(NULL)));

for(int i = 0; i < tam; i++){
int achou;
do{
pote[i] = rand() % tam;
achou = 0;

for(int j = 0; j < i; j++){

if(pote[i] == pote[j]){
achou = 1;
break;
}
}
}while(achou);
}

for(int i = 0; i < tam; i++){
printf("Tema %d >>> %s\n", pote[i], itens[i]); // I think that here is the problem
}
}

编译器说需要 char**,但 itens[] 是 char*。我尝试过改变,但没有成功。

另一件事:调用函数。

Frandom(qtd_temas, tnome[qtd_temas]); //works

Frandom(qtd_temas, tnome[]); //doesn't works

最佳答案

使用void Frandom(int tam, char itens[])代替void Frandom(int tam, char *itens[])也使用printf( "Tema %d >>> %c\n", pote[i], itens[i]);

关于c - 在函数内部使用指针时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com