我无法解决这个问题:警告:赋值来自整数而没有强制转换的指针[-Wint-conversion] - > auxcandidate = alpha [pos]
这是我的代码:
char* getkey(int minimo, int maximo, int alphalen, int index, char alpha[]);
int main()
{
int minimo = 2;
int maximo = 2;
char *alpha = "abc";
int alphalen = strlen(alpha);
char *candidata;
printf("La candidata es: ");
candidata = getkey(minimo, maximo, alphalen, 5, alpha);
//printf(" %c", candidata);
//if (candidata = "bc")
//{}
return 0;
}
char* getkey (int minimo, int maximo, int alphalen, int index, char alpha[])
{
int cociente, residuo, cont;
int i = 0;
char cand[maximo+1];
char* auxcandidate;
while (index != 0)
{
cociente = index / alphalen;
residuo = index%alphalen;
cand[i] = residuo;
index = cociente;
i+=1;
}
for (cont=i-1; cont>=0; cont--)
{
int pos = cand [cont];
auxcandidate = alpha[pos];
}
return auxcandidate;
}
有人可以解释这些警告吗?
答案 0 :(得分:4)
请注意以下一行:
auxcandidate = alpha[pos];
Alpha(pos)不是地址(指针),而是该位置的值,并且您正在尝试将其写入char指针,因此实际上您进行了转换,这是警告的来源
答案 1 :(得分:0)
就像@dear_tzvi所说,这条线是警告的来源。
您应该使用:
auxcandidate = &alpha[pos];
如果您希望auxcandidate变量指向该索引