通过函数将char传递给字符串

时间:2019-09-06 08:54:33

标签: c cs50

第2周破解,由于char和string传递错误而无法编译

#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>

char compare(char alpha, string hashed, string password, string salt, int x);
int main(void)
{
    string k ="e";
    string salt = "12";
    string crypted = crypt(k,salt);
    printf("%s\n",crypted);

    string pass[1];
    char poss [] = {'a','b','c','d','e','\0'};
    int p = strlen(poss);
    printf("%i\n",p);

    for(int i = 0; i < p; i++)
    {
        pass[0][0] = compare(poss[i], crypted, pass[0], salt, 0); //no.21
        printf("%s",pass[0]);//no.22
    }
}
char compare(char alpha, string hashed, string password, string salt, int x)
{
    password[0] = alpha;//no.27
    string q = crypt(password,salt);//no.28
    if(strcmp(q, hashed))//no.29
    {
        printf("%s",password);//no.31 
        exit(0); 
    }
    else
    {
        return alpha;//no.36
    }
}

我对程序运行方式的期望

第21行,这些args传递给一个返回char的函数

第27行,将char'a'传递到password [0] [0]位置。将password [0]字符串设为'a'

第29行,strcmp函数将字符串q与哈希值进行比较。

第29行,因为它为false,所以它将转到第36行,该行将char'a'返回到第21行的pass [0] [0]中。

第22行,将打印出“ a”。

错误:

第27行错误:下标的值不是数组,指针或向量

第28行错误:指针转换不兼容的整数,将'char'传递给'const char *'类型的参数;

第31行错误:格式指定类型为'char *',但参数的类型为'char'

0 个答案:

没有答案
相关问题