Errata for C Strings chapter


In the introduction to the challenge, Aaron shows the code that should appear in main(), but it has a typo. It should read:

printf("\"%s\" has %d spaces\n", sentence, spaceCount(sentence));

(the n is missing after the final backslash in the book)


i have the solution working but i’m wondering about a Compiler warning

The above fragment generates a compiler warning [color=#FF4000] passing ‘const char *’ to parameter of type ‘*char’ discards qualifiers[/color]
for spaceCount(sentence) part of the above printf() .

If I remove the ‘const’ from sentence declaration in main() or specify ‘const’ for spaceCount()'s argument, the Compiler warning goes away.

Anyone have ideas? Thanks!

My spaceCount() function looks like this:

[code]int spaceCount(char *sentence)
int ctr = 0;

for (int i=0 ; sentence[i] != '\0'; i++ )
    if (sentence[i] == 0x20)
return ctr;



When you have a function that takes a character string, you can declare it like this

int countVowels(char *s)
or like this:

int countVowels(const char *s)
The latter says “I’m taking a pointer to some characters, but I promise not to change them!”

It this case, that sort of declaration would be more accurate. And when you pass a literal string, the function isn’t allowed to change it. So the compiler is saying “Don’t you want that const there?”


that makes sense.


Not really problem, but there’s another typo, at least in the ebook (in iBooks it’s on the 2nd page of this chapter). “What happened to the to the rest?” (the words “to the” probably shouldn’t be there twice). Something to correct for the next edition. :wink:


Darn! Thanks for catching that. It will be fixed in the next printing.


Errm… my ebook isn’t printed :mrgreen: