C编译器认为char类型是int类型 - clang编译器

标签 c compiler-errors type-conversion cs50

所以我正在学习计算机科学方面的 edX 类(class)之一(不是为了学分,只是为了通过讲座来帮助自学),高级问题集与解密密码有关。为了更好地理解用于进行加密的函数 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);
    }

最佳答案

strcat 用于连接字符串而不是2个单独的字符。 “whatever1”会将连接的字符串保持在一起。当您只需拥有一个 2 字节字符串时,为什么要使用 -dangerous- strcat 操作?只需使用类似的东西:

char salt[3];
salt[0]=toASCII_New(i); /* why the casting to char ? you already returned a char... */
salt[1]=toASCII_New(j);
salt[2]='\0'; /* to terminate the string properly */

关于C编译器认为char类型是int类型 - clang编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338018/

相关文章:

excel - 在另一台机器上运行时编译错误

android - 如何舍入长数字并将其显示为android中的字符串?

c - 将 C 字符串转换为所有一种情况处理器的最快方法

c - 如何确定 C 与数组的互操作性

c - 使用 MPI 的 C 程序中的段错误

compiler-errors - 编译三个文件fortran 77 “Line too long”

ios - 为什么我的正则表达式中存在多个捕获组会使我的应用程序崩溃?

swift - 无法将 'ClosedRange<Int>' 类型的值转换为预期的参数类型 'Range<Int>'

c - 如何在 C 中对 .txt 文件内的数据进行排序

将两个 32 位 float 转换为一个 64 位数字,反之亦然