Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

K&R

Name: Anonymous 2011-10-04 15:45

Well I just finished reading K&R. I got it for my machine structures class in Fall '09, but we were never assigned any exercises out of it so I never bothered reading it because I already knew C. Since then, I've read several mentions of what a great book it is from the likes of Linus Torvalds and Rob Pike among others, so I decided to give it a read.

My impression: what...the...fuck? Are you serious? The examples in K&R are often cited as ``the official way to code in C,'' but 90% of them are badly designed on purpose so they only utilize features that have been introduced up to that point in the book. That may make it a great beginner's book, I guess, but as a reference or example of best practices, lolno.

Name: Anonymous 2011-10-04 20:14

With regards to your example, I'd use the former (char s[]) to render clear that the function expects an array of some sort, specially if the array has a fixed size (in which you would put the array size in the declaration), and the latter otherwise.

Well the function's called print, s is obviously a string. When dealing with output, it's pointless to note the size of the array the string is stored in because it should be terminated by a null character, and if it's not, you're still going to end up printing junk even if you stop at the end of the array.

I would personally write such a function:

void print(char *s) {
    while (*s != '\0')
        putchar(*s++);
}


Declaring char s[] in this case wouldn't be as clear. In fact, K&R says it's illegal, but gcc will compile it without warning.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List