Live data from Hacker News

To become a good C programmer (2011)

fabiensanglard.net

11–20 of 106 posts

Re: To become a good C programmer (2011)

#11
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…

Fabien is hinting at the confusion: sizeof(&array[0]) != sizeof(array)

&array[0] and array decay to the same thing, the pointer to the first element if they are used in an expression. But sizeof gives a different result, because array+0 'decays' to a pointer, and array doesn't.

Re: To become a good C programmer (2011)

#12
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 first learned C from K&R, but I'm not really that big of a fan. I find the style of inter-mixing new information in the middle of examples kind of hard to follow. I prefer to have the basics set out in front, and than get a bunch of examples demonstrating them. I'm sure people with different learning styles have an easier time with it, though.

Re: To become a good C programmer (2011)

#13
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 Second Ed. was published going on two decades after C first emerged. Already many versions of Unix and the applications to run on them were written at the time of K&R Second. You can hardly say that little was known about software design with C at the time it came out.

It won't cover C99, C11, or modern practices to mitigate buffer overflows. But it is still an excellent introduction to the fundamentals of C. It's also aimed at people who already know how to program in, say, Pascal, so is probably best not approached as a rank beginner.

Re: To become a good C programmer (2011)

#14
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?

> thoughts ?

well, there is a 'ModernC' book by Jens Gustedt of INRIA available here: http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf

which might suit your fancy ?

Re: To become a good C programmer (2011)

#15
"And no good book is as good as disassembly output."

[x] Strongly agree [ ] Agree [ ] Neutral [ ] Disagree [ ] Strongly disagree

For me, C, i.e., GCC, is most useful as a faster way to generate assembly for a particular CPU than typing it out from scratch. I use GCC as a code generator. I'd like to see more free asm code generators, but I am not holding my breath.

I do appreciate C as a medium for distributing reasonably efficient software.

Re: To become a good C programmer (2011)

#16
post #15

"And no good book is as good as disassembly output." [x] Strongly agree [ ] Agree [ ] Neutral [ ] Disagree [ ] Strongly disagree For me, C, i.e., GCC, is most useful as a faster way to generate assembly for a particular CPU than typing it out from scratch. I use GCC as a code generator. I'd like to see more free asm code generators, but I am not holding my breath. I do appreciate C as a medium for distributing reason…

I don't really understand what this means. How is that different from how anyone else uses a compiler?

Re: To become a good C programmer (2011)

#17
The more I learn C the more I hate it. At first it seems simple and easy but reading "Expert C Programming" is reading a laundry list of what's really messed up with the language. 80% of the problems would be solved by some sane syntactic sugar that compiles down to C

Re: To become a good C programmer (2011)

#18
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 el…

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

I really like the idiom for passing sized arrays suggested at the end of that LKML thread[1]: pass them by reference!

  void func(int (*arr)[256])
  {
    printf("arr size: %ld\n", sizeof(*arr));
  }

  int main(void)
  {
    int array[256];
    func(&array);
  }
[1] https://lkml.org/lkml/2015/9/7/147

Re: To become a good C programmer (2011)

#20

The more I learn C the more I hate it. At first it seems simple and easy but reading "Expert C Programming" is reading a laundry list of what's really messed up with the language. 80% of the problems would be solved by some sane syntactic sugar that compiles down to C

Why are you learning it?
Post reply on HN