Live data from Hacker News

To become a good C programmer (2011)

fabiensanglard.net

1–10 of 106 posts

Re: To become a good C programmer (2011)

#2
This is the first time I have seen sizeof used like this:

  sizeof( &array[0] )
This looks equal to:

  sizeof( array )
at first glance, which would give the size of the entire array in bytes, but of course the &array[0] expression is really:

  &*( array + 0 )
which simplifies to:

  array + 0 
which is a pointer. And using sizeof on it gives the size of a pointer to int.

Edit: (&* array) will also give a pointer.

---

This is just a really convoluted way to write 2:

   &array[2] - &array[0]

   &*(array+2) - &*(array+0)

   (array+2) - (array+0)

   2 - 0
Again I have never seen this written in such fashion.

Re: To become a good C programmer (2011)

#3
K&R is often recommended, and it's certainly fun to read and accessible. But I've also heard it's outdated, and doesn't rally focus much on modern C software design, mostly because the world knew little about it when K&R was written.

Thoughts?

Re: To become a good C programmer (2011)

#4
post #3

K&R is often recommended, and it's certainly fun to read and accessible. But I've also heard it's outdated, and doesn't rally focus much on modern C software design, mostly because the world knew little about it when K&R was written. Thoughts?

It is still relevant due to the fact that K&R C is still extremely (but not completely) forward compatible with even the most modern c++ standards. Many software design best practices have changed since then, but if you read a more modern book some of the first principles of the C language will have to be either repeated or omitted by a modern book.

Re: To become a good C programmer (2011)

#5
post #3

K&R is often recommended, and it's certainly fun to read and accessible. But I've also heard it's outdated, and doesn't rally focus much on modern C software design, mostly because the world knew little about it when K&R was written. Thoughts?

K&R is still a great book but there are also other great books. Programming in C by Kochan and A Modern Approach by King are two fantastic books and much more suited to a true beginner than K&R is IMHO. Also Head First C and 21st Century C (second edition) are great books to read.

The exercises in K&R are superb though and I highly recommend taking the time to do them all while you read K&R which I still feel you should, it is a small book so shouldn't take long to read.

Re: To become a good C programmer (2011)

#7
post #3

K&R is often recommended, and it's certainly fun to read and accessible. But I've also heard it's outdated, and doesn't rally focus much on modern C software design, mostly because the world knew little about it when K&R was written. Thoughts?

I think the style should be avoided; code shouldn't be compact and variable names should be descriptive.

Artificial example:

    for( int i = 0 ; i 
should be:

    for( int count = 0 ; count 
It may be allowed to use i in place of count here, but this is the only place where single name variables should be permitted and only if i is really just a simple array index iterator.

Re: To become a good C programmer (2011)

#8
post #2

This is the first time I have seen sizeof used like this: sizeof( &array[0] ) This looks equal to: sizeof( array ) at first glance, which would give the size of the entire array in bytes, but of course the &array[0] expression is really: &*( array + 0 ) which simplifies to: array + 0 which is a pointer. And using sizeof on it gives the size of a pointer to int. Edit: (&* array) will also give a pointer. --- This is j…

The array->pointer "decay" (as the standard calls it) has 3 exceptions, of which two are when an array is the operand of "sizeof", and when it is the operand of "&". (The third involves a string initialiser, which is not relevant here.)

So your reasoning is not quite correct, it should really be that you think of

    sizeof( &array[0] )
as being

    sizeof( &(something) )
where "something" could be of any type T, and so the '&' operator yields "pointer-to-T", to which sizeof will yield the size of a pointer.

Re: To become a good C programmer (2011)

#9
post #2

This is the first time I have seen sizeof used like this: sizeof( &array[0] ) This looks equal to: sizeof( array ) at first glance, which would give the size of the entire array in bytes, but of course the &array[0] expression is really: &*( array + 0 ) which simplifies to: array + 0 which is a pointer. And using sizeof on it gives the size of a pointer to int. Edit: (&* array) will also give a pointer. --- This is j…

Here are the relevant parts of the C standard:

C89 3.3.3.4

"The sizeof operator... When applied to an operand that has array type, the result is the total number of bytes in the array."

C89 3.2.2.1

"Except when it is the operand of the sizeof operator or the unary & operator, or is a character string literal used to initialize an array of character type, or is a wide string literal used to initialize an array with element type compatible with wchar_t, an lvalue that has type ``array of type '' is converted to an expression that has type ``pointer to type '' that points to the initial member of the array object and is not an lvalue."

Additionally, here is a thread of Linus Torvalds pointing out even more of the confusing nature of arrays and sizeof in C:

https://lkml.org/lkml/2015/9/3/428

Re: To become a good C programmer (2011)

#10
I found that, after learning the basics of the language via K&R or a similar book, the best way to get a good understanding of C and its weird corner cases and eccentricities is just to go through the comp.lang.c FAQ page. http://c-faq.com/index.html

It's pretty comprehensive, and I found the level was pretty good for a "not total newbie, but still not familiar with the subtilties" level that can be kind of hard to find resources for.

Post reply on HN