简体   繁体   English

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

[英]C Compiler thinks char type is int type - clang compiler

So I am going through one of those edX courses in computer science (not for credit, just to have lectures to help self-learning) and the advanced problem set has to do with decrypting passwords. 因此,我正在学习其中一门edX计算机科学课程(不是为了获得学分,而是为了帮助自学而开设讲座),而高级问题集与解密密码有关。 To get a better understanding of the function crypt() used to make the encryptions, I made the following code to figure out what was being used for the "salts" string (the man crypt() command in the terminal said: ("salt is a two-character string chosen from the set [a-zA-Z0-9./]. This string is used to perturb the algorithm in one of 4096 different ways.")) Please tell me if you can see why I am getting this error. 为了更好地理解用于进行加密的函数crypt(),我编写了以下代码来弄清楚“ salts”字符串所用的内容(终端中的man crypt()命令说:(“ salt是从[a-zA-Z0-9./]集中选择的两个字符的字符串。此字符串用于以4096种不同方式之一干扰算法。“))请告诉我是否知道我为什么得到这个错误。

Below is the code I used. 下面是我使用的代码。 I am getting this error when attempting to compile: 尝试编译时出现此错误:

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);

Code: 码:

    #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 is for concatenating strings not 2 separate chars. strcat用于连接字符串,而不是2个单独的字符。 "whatever1" will hold the joined string together. “ whatever1”将连接的字符串连接在一起。 Why using -dangerous- strcat operations when all you have to do is to have a 2-byte string ? 当只需要一个2字节的字符串时,为什么要使用-dangerousstrcat操作? Just use something like: 只需使用类似:

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 */

Here are some general advices, as the entire code needs to be refactored: 以下是一些一般性建议,因为整个代码都需要重构:


Function toASCII_New should be rewritten as follows: toASCII_New函数应重写如下:

char toASCII_New(int c)
{
    if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'))
        return (char)toascii(c);
    return 'a';
}

Function checker should be rewritten as follows: 功能checker应重写如下:

int checker(int first, int second, char salt[3])
{
    for (int i=first; i<=first+25; i++)
    {
        for (int j=second; j<=second+25; j++)
        {
            salt[0] = toASCII_New(i);
            salt[1] = toASCII_New(j);
            password = crypt(word,salt);
            if (strcmp(password,"50zPJlUFIYY0o") == 0)
                return 1;
        }
    }
    return 0;
}

Function main should be rewritten as follows: 函数main应该重写如下:

int main()
{
    char salt_AA[3] = {0};
    char salt_Aa[3] = {0};
    char salt_aA[3] = {0};
    char salt_aa[3] = {0};
    int  salt_AA_OK = checker('A','A');
    int  salt_Aa_OK = checker('A','a');
    int  salt_aA_OK = checker('a','A');
    int  salt_aa_OK = checker('a','a');
    printf("AA: %s\n",salt_AA_OK? salt_AA:"failed");
    printf("Aa: %s\n",salt_Aa_OK? salt_Aa:"failed");
    printf("aA: %s\n",salt_aA_OK? salt_aA:"failed");
    printf("aa: %s\n",salt_aa_OK? salt_aa:"failed");
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCC编译器警告:格式&#39;%c&#39;需要类型为&#39;char *&#39;的参数,但参数2为类型&#39;int *&#39;[-Wformat] - GCC compiler warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat] Clang编译器的C enum的数据类型是什么? - What's the data-type of C enum of Clang compiler? 在c编译器中输入类型 - type cast in c compiler c类型等价的编译器 - Compiler of c type equivalence 当我运行我的 C 程序时,我的编译器返回“从不兼容的指针类型‘char *’[-Wincompatible-pointer-types] 分配给‘int *’” - My compiler returned "assignment to 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]" when I ran my C program C编译器:“类型&#39;char *&#39;和&#39;int&#39;不是赋值兼容的” - 但是没有int? - C compiler: “Types 'char *' and 'int' are not assignment-compatible” - but there's no int? 调用 sprintf 的编译器错误:“应为 &#39;char *&#39;,但参数的类型为 &#39;char&#39;” - Compiler errors calling sprintf: “expected 'char *' but argument is of type 'char'” 编译器返回错误“ char *(int)”的间接寻址级别与“ int()”不同,即使返回的数据似乎与函数返回类型匹配 - Compiler returns error “char *(int)' differs in levels of indirection from 'int ()'” even though data returned seems to match function return type ac程序中的编译器错误:类型为void *的间接寻址 - compiler error in a c program:indirection on type void* C-编译器错误:取消引用不完整类型的指针 - C - compiler error: dereferencing pointer to incomplete type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM