Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

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

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

#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.
", student, course, numDays);
}

int main(int argc, const char * argv[])
{
    // insert code here...
    congratulateStudent("mark", "Cocoa", 5);
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
void congratulateStudent(char *student, char *course, int numDays)

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

and char student this means that student is a char type

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

In your code you are calling the congratulateStudent with input parameter string "mark". So to support this string the input parameter student should be defined as pointer of string

so you are missing the asterisk in the definition of student

The same thing for course


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...