字符串变量赋值给指针错误

string reading_lev(int a, int b, int c)
{
    float L = (a / b) * 100;
    float S = (c / b) * 100;
    float index = 0.0588 * L - 0.296 * S - 15.8;
    if (round(index) <= 16 && round(index) >= 1)
    {
        string val = printf("Grade %f", index);
    }
    else if (round(index) > 16)
    {
        string val = printf("Grade 16+");
    }
    else
    {
        string val = printf("Before Grade 1");
    }
    return val
}

错误在第一个if块中。有涉及的CS50库。

错误:使用类型为'int'的表达式初始化'string'(aka'char *')的指针转换不兼容的整数[-Werror,-Wint-conversion]
评论
  • liste
    liste 回复

    该错误信息不言自明。

    printf() returns an int, you cannot store that in a char*.

    也就是说,您还有其他多个问题:

    • The return statement uses a block scope variable, which is out of it's scope.
    • The return statement is missing a ; - syntax error.