C语言 如何修复错误格式指定类型“char *”但参数的类型为“char”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14039329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:52:25  来源:igfitidea点击:

How to fix error Format specifies type 'char *' but the argument has type 'char'

objective-ccxcode

提问by sharataka

I get a warning saying: "Format specifies type 'char *' but the argument has type 'char'" for the student variable. I am copying/pasting the code out of a book into xcode and am not sure how to fix this. The only thing that prints in the console is "(lldb)". Any advice

我收到一条警告说:“格式指定了类型 '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;
}

回答by MOHAMED

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

the %smeans that you are going to print a string ( array of chars)

%s意味着你要打印一个字符串(字符数组)

and char studentthis means that student is a chartype

char student这意味着,学生是一个char类型

so student here is not a pointer to a string

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

In order to change the student type from char to a string pointer you have to add asterisk to student char *student

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

In your code you are calling the congratulateStudentwith input parameter string "mark". So to support this string the input parameter studentshould be defined as pointer of string

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

so you are missing the asterisk in the definition of student

所以你在学生的定义中缺少星号

The same thing for course

同样的事情对于 course

回答by Adeel Ahmed

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

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

Use Function signature like because you are passing string as argument to function in mainbut function has character type argument..

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