Live data from Hacker News

Beej’s Guide to C Programming [pdf]

beej.us

91–100 of 178 posts

Re: Beej’s Guide to C Programming [pdf]

#91
post #7

I've enjoyed this guide a number of times but each time I hit a brick wall trying to understand pointers. I'm still keen to learn but it just doesnt 'click' for me... Edit: poor grammar

1) A pointer is a memory address of an object.

2) Pointer arithmetic takes into account the size of the type pointed to.

3) The asterisk operator gives you access to the object.

4) The ampersand operator gives you the pointer to the object.

If you understand this, you understand pointers. (The arrow operator is syntactic sugar.)

Re: Beej’s Guide to C Programming [pdf]

#92
post #86

Earlier quoted context omitted.

Beej himself lists this as an 'alpha-quality document' on the download page [0] and if I remember correctly, it has been so for years. Wonder why this is posted here on HN. [0]: http://www.beej.us/guide/bgc/

I think posts like this are handy just for the extra exposure an alpha document might need; there's a lot of good feedback and discussion here that hopefully beej comes across someday and can finish the book

On further observation the git repository of the book [0] seems to be quite active. Maybe this book might be finished after all.

[0]: https://github.com/beejjorgensen/bgc

Re: Beej’s Guide to C Programming [pdf]

#94
There was a "C Unleashed" book, a massive tome of 1000+ pages written by many famous programmers, many of them who where quite active in comp.lang.c, like Richard Heathfield and CB Falconer, had quite insightful material in it.

Any one remember the heyday of comp.lang.c? I wonder what goes on in there now.

Re: Beej’s Guide to C Programming [pdf]

#95
post #85
post #57

Earlier quoted context omitted.

But int-pointer is the type of it. It makes no sense that it “sticks” to the variable name.

The reason the asterisk naturally "sticks to" the name is simply the possibility to write a series of declarations like this: int a, *b, (*c)(int);

Which is a questionable saving of 2 lines and a few characters in a language where huge repetitions are a given, due to lack of templates/generics whatever.

Re: Beej’s Guide to C Programming [pdf]

#96
post #27

Jens Gustedt's "Modern C" ( https://modernc.gforge.inria.fr ) is an excellent resource as well.

What does "modern" C imply? AFAIK, there are not very many new language features; is it about organizing code differently than what one would learn from K&R?

Re: Beej’s Guide to C Programming [pdf]

#98
post #7

I've enjoyed this guide a number of times but each time I hit a brick wall trying to understand pointers. I'm still keen to learn but it just doesnt 'click' for me... Edit: poor grammar

Pointers are easy and fun, just keep experimenting with them. What is essential is understanding that they're nothing more than like a piece of paper containing where something is instead of being part of that something, and they're the same size of the address registers of the underlying hardware as they must be able to point everywhere in the addressable memory. Therefore, on a 32 bit system pointers will be 32 bits long, on a 64 bit system they will be 64 bits long, etc. no matter what they point to.

That is, both a pointer to a single byte and a pointer to a 10GB memory chunk will be of the same size because they just hold a memory location whose value represents where that data starts. Therefore, declaring pointers to a certain type doesn't change their size at all, it just becomes handy when one needs to go back and forth in a memory area in which objects of that type are stored one after another, so that incrementing or decrementing the pointer by a number actually means that number times the size of the objects. Imagine asking for directions to someone and he replies "3rd door" or "3rd building" or "3rd block"; he gave you a pointer that is always the same size, but how much you have to walk will depend on the destination (size).

Once grasped the above, it should become a lot more easy; I had the same problems, then one day had a flash and they became totally clear (and fun).

It may be of help experimenting with a debugger, or simply printf-ing all values a pointer assumes when declared, assigned, incremented/decremented etc. Keep also track of the pointed data values, changing it instead of the pointer, or the other way around, are common mistakes.

An old small command line program like "cdecl" can help a lot to understand complex declarations, and someone has even made an interactive webpage around it (cdecl.org).

example:

cdecl> explain char ((x())[]) () declare x as function returning pointer to array of pointer to function returning char

Re: Beej’s Guide to C Programming [pdf]

#99

I stumbled upon this gem a while ago [0] while looking for a decent tutorial and reference to C: Stuff that should be avoided: [...] Beej's Guide to C: http://beej.us/guide/bgc/output/html/singlepage/bgc.html Full of mistakes. [...] Could someone confirm this? I've seen a lot of threads here on HN praising beej's guides so I am somewhat confused. [0] http://www.iso-9899.info/wiki/Main_Page edit: Formatting

On a quick skim of some introductory parts I found: > When you have a variable in C, the value of that variable is in memory somewhere, at some address. Of course. After all, where else would it be? It would be in a register. Of course. Or it would be eliminated by a compiler optimization. Of course. Same error later on: > When you pass a value to a function,a copy of that value gets made in this magical mystery worl…

> It would be in a register. Of course. Or it would be eliminated by a compiler optimization

As long as you’re taking, and using, the address of that variable, it’s almost guaranteed to be in memory. Even if it won’t, the compiler guarantees the output of the program will be equivalent to unoptimized code.

> arguments will not be passed via the stack

I’m not sure explaining nuances of various calling conventions, and how they differ across processors and OSes, is useful information in a document about C and targeted towards beginners.

You’re talking about things which are underneath C in the abstraction layer hierarchy. The abstraction has many layers, the lowest one being quantum physics. One has to stop somewhere, and this article decided to stop at C, as opposed to assembly.

Re: Beej’s Guide to C Programming [pdf]

#100
post #27

Jens Gustedt's "Modern C" ( https://modernc.gforge.inria.fr ) is an excellent resource as well.

What does "modern" C imply? AFAIK, there are not very many new language features; is it about organizing code differently than what one would learn from K&R?

C99 I would imagine: https://en.wikipedia.org/wiki/C99
Post reply on HN