我正在尝试将26个字母的密码中的单个字符转换为适当的索引字符。我试图将字符"oc"转换为整数,因为数组"cipher"需要一个整数作为索引,但我遇到了无法理解的错误。
char conversion(char c, string cipher)
{
// Only change alphabetical characters
if (isalpha(c) == 0)
{
return c;
}
char oc;
// If upper case
if (isupper(c))
{
oc = c - 65;
int ocint = atoi(oc);
oc = cipher[ocint];
oc = oc + 65;
return oc;
}
我得到错误:
error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
int ocint = atoi(oc);
^~
&
/usr/include/stdlib.h:104:30: note: passing argument to parameter '__nptr' here
extern int atoi (const char *__nptr)
什么是"带&"的地址意思是
还有'__nptr'?
atoi
用于转换整个字符串。如果您想将单个字符A到Z转换为相应的数字,那么oc = c - 65;
已经做到了。最好写入CCD_ 3。
因此,在这种特定情况下,您甚至不需要atoi
。在一般情况下,也不应该使用atoi
,因为它是一个设计错误的函数,没有错误处理。始终使用strtol
。