kevinius
kevinius

Reputation: 4620

passing char to function in c (is the book wrong?)

I'm reading the book "Objective-C Programming The Big Nerd Ranch Guide".


They give out this code:

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);
}

and call it with this:

congratulateStudent("Mark", "Cocoa", 5);

This gives me this warning:

Format specifies type 'char *' but the argument has type 'char'

Is the book wrong?

Upvotes: 1

Views: 5247

Answers (4)

Coding Mash
Coding Mash

Reputation: 3346

There might be a typo.

Char means only one character in single quotes, as 'a'.

A constant string is in double quotes and decays into a char* or character pointer, like this.

"Hello World"

Upvotes: 2

P.P
P.P

Reputation: 121397

Yes, that's not correct. Perhaps a print error. Just make them pointers:

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

Technically, it's undefined behaviour in C to pass incorrect format string to printf.

Upvotes: 1

AliSoftware
AliSoftware

Reputation: 32681

Yes the book has a typo.

You should use char* instead of char for both parameters of your C function

Upvotes: 0

Jimmy Johnson
Jimmy Johnson

Reputation: 908

Yes if this is what the book says to do it is definitely a mistake it should be char * as the parameters in the method like the warning says.

Upvotes: 1

Related Questions