Errata for C Strings chapter


#1

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)


#2

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)
   {
        ctr++;
    }
}
return ctr;

}[/code]


#3

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?”


#4

that makes sense.


#5

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:


#6

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


#7

Errm… my ebook isn’t printed :mrgreen: