【发布时间】:2018-04-17 10:48:53
【问题描述】:
在我尝试使用它来显示字符串之前,我有一个 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
【问题讨论】:
-
tnome是如何在调用者中声明的? -
此外,用于打乱整数数组的算法效率极低。它应该是 O(n),但你所拥有的是 O(n**3) 平均值(理论上是无限的)。
-
@Tom Karzes ''char* tnome[qtd_temas];'' 更具体地说:我使用 malloc 设置 itens 的大小(动态分配),然后我有一个分配内存的指针我将收到的每个字符串的空格。好像是:iten [any number] = {"string", "string", "string"}。
标签: c function pointers char void