【问题标题】:How to fix error Format specifies type 'char *' but the argument has type 'char'如何修复错误格式指定类型'char *'但参数的类型为'char'
【发布时间】:2012-12-26 10:33:17
【问题描述】:

我收到一条警告说: “格式指定类型'char *',但参数的类型为'char'”用于学生变量。我正在将书中的代码复制/粘贴到 xcode 中,但不知道如何解决这个问题。在控制台中打印的唯一内容是“(lldb)”。任何建议

#include <stdio.h>

void congratulateStudent(char student, char course, int numDays)
{
    printf("%s has done as much %s Programming as I could fit into %d days.\n", student, course, numDays);
}

int main(int argc, const char * argv[])
{
    // insert code here...
    congratulateStudent("mark", "Cocoa", 5);
    return 0;
}

【问题讨论】:

    标签: objective-c c xcode


    【解决方案1】:
    void congratulateStudent(char *student, char *course, int numDays)
    

    %s 表示您要打印一个字符串(字符数组)

    char student 这意味着学生是char 类型

    所以这里的学生不是指向字符串的指针

    要将学生类型从 char 更改为字符串指针,您必须将星号添加到 student char *student

    在您的代码中,您使用输入参数字符串"mark" 调用congratulateStudent。所以为了支持这个字符串,输入参数student应该被定义为字符串的指针

    所以你错过了学生定义中的星号

    course 也一样

    【讨论】:

      【解决方案2】:

      void congratulateStudent(char *student, char *course, int numDays)

      使用函数签名,因为您将字符串作为参数传递给main 中的函数,但函数具有字符类型参数..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-29
        • 2020-08-02
        • 2021-12-08
        • 2017-09-04
        相关资源
        最近更新 更多