【发布时间】:2014-08-16 07:38:52
【问题描述】:
所以我正在学习其中一门计算机科学的 edX 课程(不是为了学分,只是为了有讲座来帮助自学),而高级问题集与解密密码有关。为了更好地理解用于进行加密的函数 crypt(),我编写了以下代码来确定“salts”字符串使用的是什么(终端中的 man crypt() 命令说:(“salt是从集合 [a-zA-Z0-9./] 中选择的两个字符的字符串。此字符串用于以 4096 种不同方式之一扰乱算法。")) 请告诉我你是否明白我为什么收到此错误。
下面是我使用的代码。尝试编译时出现此错误:
test.c:37:33: error: incompatible integer to pointer conversion passing 'char' to
parameter of type 'char *'; take the address with & [-Werror,-Wint-conversion]
char *salt = strcat(whatever1,whatever2);
代码:
#define _XOPEN_SOURCE //this is needed for the unistd and crypt stuff
#include <stdio.h>
#include <unistd.h> //this lets me do the crypt() fcn
#include <cs50.h> //just lets me use string as a type among w/some input fcns and is made by the course
#include <string.h>
#include <ctype.h> //this lets me use toascii()
char *word = "caesar";
char *password;
char toASCII_New(int i) //attempt to turn input to HEX then corresponding ASCII character
{
for(int num = i; num<= 172; num++)
{
for(int hex_num = 0x41; hex_num <= 0x7a; hex_num++)
{
if(num == hex_num)
{
char letter = (char) toascii(hex_num); //should convert hex to ascii here
return letter;
}
}
}
char backup = 'a';
return backup;
}
string checker(int first, int second) //Checks the inputs to see if the can be the correct SALTS input to crypt
{
for (int i = first; i<=first+25; i++)
{
for(int j = second; j<=second+25; j++)
{
char whatever1 = (char) toASCII_New(i);
char whatever2 = (char) toASCII_New(j);
char *salt = strcat(whatever1,whatever2);
password = crypt(word,salt);
int compare = strcmp(password,"50zPJlUFIYY0o"); // this is what i'm really checking
if (compare == 0)
return salt;
}
}
return "no";
}
int main(void)
{
string AA = checker(65,65);
string Aa = checker(65,97);
string aA = checker(97,65);
string aa = checker(97,97);
printf("AA: %s\nAa: %s\naA: %s\naa: %s\n",AA,Aa,aA,aa);
}
【问题讨论】:
-
你想用
toASCII_New实现什么?我认为您首先不应该使用toascii。而且,你为什么要把char转换成char(当调用toASCII_New时)?还有更多类似的细节,但它们将有助于阅读和理解您的代码。 -
C 中的
string是什么? -
@barakmanos 在代码中,第 4 行。
-
@Nelxost:是的,我看到了,但请注意旁边的“由课程制作”评论。我们是不是应该猜测
string结构是如何在那里实现的??? -
toASCII_New()函数表明缺乏对char、int和“十六进制值”的理解。整个函数体可以替换为if (('A' <= i) && (i <= 'z')) return i; return 'a';嵌套循环的原因让我无法理解。
标签: c compiler-errors type-conversion cs50