Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yet another broken C pointers guide out there to confuse people.


What is broken about it? I am happy to make corrections.


Minor points:

- & is called the "address of" operator, and saying "The & is the reference operator and is used to reference a memory address" sounds just strange.

- In C, a reference is just a way to indirecty access an object, which means that a pointer is a reference. The C language doesn't define references like C++ does (as another name for an object). In C, references are much informal (the conceptual definition is used; as in a means to access something else).

I am going to be honest with you, I read the beginning. Found it strange, then looked at the topics. Saw you had a "pointers and arrays" section. Read it (it's the part where people get confused more). You seem to make the same mistakes. I didn't read the rest.

These are not minor problems, IMO.

Here is a quote from the pointers and arrays section:

"Imagine an array variable like a pointer that cannot be changed that holds the memory address of the first element of the array it points to. Even though the array variable holds a memory address, you cannot assign a pointer variable to an array variable, even if the pointer variable actually points to the same or a different array. You also cannot assign one array variable to another."

Arrays are not pointers. And they don't hold memory addresses. An array is a continuous block of memory holding as many objects as you specified, all of the same type. You should explain "value context" and "object context" and tell people that when an array name is used in a value context, the value you get is a pointer to its first element. This is not due to "arrays are pointers" or "arrays hold pointers". Arrays are not pointers; and they only hold pointers if the element type is of a pointer type (however, in this case, the pointer(s) is(are) its element(s)).

You should explain what it means "arrays values are pointers to their first elements", and focus on "value". If you take the sizeof operator or the address of operator, they operate on object context (not on value context), then you will "reveal" the "true nature" of the array.

To really be able to explain pointers, you should explain other things first. Which is very hard to do in a small blog post. I suggest you make a series of posts; and try to make then correct, w/o getting bogged down with definitions of C terminology (which idk how to avoid). Terminology such as lvalue, rvalue, name, value context, object context, value of something, object, identifier (in C, identifiers and names are 2 different terms), and some others.

Out of curiosity... Can I ask you why you're posting about C?


You have some valid points. I clarified the arrays section. I am just attempting to show some simple examples. Wasn't trying to do anything in depth. Not the easiest things with pointers of course.


Very few people have reasons to use C. As far as I know, must people do not want to use C. Those who want or have reason to, should go in depth. If you're trying to avoid going deep on the subject, then, forgive my intrusion, maybe you should be focusing on something else.

Some languages you can use by just learning a little bit here and there, supperficially. C is not one of these.


I'd wager a good number of people don't want to use C because many intro tutorials are too pedantic, too clever, and/or too intimidating. C doesn't have to be hard. I agree that the OP should work to retain 100% accuracy in his article, but I disagree that he should fledge out "the truth" to its fullest extent for a beginner. There's always a level deeper you can go in learning how pointers/memory/hardware/transistors work. Deciding the extent of information you wish to impart before letting the beginner do some programming is a very delicate decision.


One major problem, which I think is related to this you're saying is that C is used as if it was a beginner's language, but it isn't.

C was made so that it could be simply compiled to assembly. Also important, it had high portability "in mind". The mix of these two design goals is that compilers could exist for many platforms. And not just that, it's a language that was made also so that a large part of the UNIX operating system could be re-written in.

As some additional, although you can interpret C, that's not how it's usually used. In teaching/learning, people use optimizing (professional) compilers such as GCC. Which leads to a much non-interactive environment.

Put these 2 design goals together and you're bound to realize a language which is not beginner friendly because lots of things are there, or not, for reasons (a) to help the compiler writers; (b) due to portability on some machine of the 70's or 80's; and (c) of being a language for OS development. Add this with the fact that the tools around the language are not so interactice, you get something really beginner unfriendly.

C is not simple to learn, to use or to get good at. Compilers take advantage of what was put in the standard in very bizarre ways. Which means that if your code is wrong, even a little bit, it's likely that you get bugs, terrible ones. The optimizing compilers (good for professional use; bad for leaning) are usually the ones which will lead to the strangests bugs. Again, not beginner friendly.

It's often that people think they're good at C, and that they write portable C code: they usually are not and don't. C is very good at deceiving people.

Unfortunately, C is not a language that you can just pick and start solving your problem right away. Many more ordinary statements can fail for non-obvious reasons, which is not so common in other languages.

I am not implying that you don't know all this. I am though, implying that you should take this into consideration when you talk about learning C, and beginner programmers approaching C, and even beginners in C from other languages.

There are good books which are not difficult, and do not let go of preciseness, like K&R2.


"C is not simple to learn, to use or to get good at." The R&K is a very pedagogical book. This is really easy to read. The exercises at the end of chapter are excellent. Even a beginner can reach a very good level in C by studying this single book.

There are many trickiness (most of them result from recent standards), but beginners should simply avoid them using a sane programming style.


Honestly, why don't you re-iterate the known tutorials about these things? Make a blog post saying "here are these old C tutorials, which are amazing; and some people seem to have forgotten these things" and post them. Lots of comp.lang.c posts are interest Chris Torek's stuff are interesting. If you go to the ##c channel, at irc.freenode.com, they have a wiki link on the topic. That wiki has links to many tutorials. Re-iterate these things. And, please, recommend people using something other than C =)


It would be super cool if you would just point to what you think is the better stuff with a link.


Sorry. I don't know why I didn't do it.

http://www.iso-9899.info/

This is that wiki page I mentioned. It has links to all sorts of places. All articles I was talking about, I found in there.

It has an articles page:

http://www.iso-9899.info/wiki/C_gotchas

And it has some recommendations on many things, including other artigles.

http://www.iso-9899.info/wiki/Usenet

Check out the "Additional materials part":

http://www.iso-9899.info/wiki/Main_Page#Additional_materials

Anyway, wander a little bit in that page. The books recommendations are great, even though they recommend Deitel & Deitel C book, which is not bad, but it's sort of shallow.


Thanks, I appreciate that.


This Chris Torek guy has a bunch of articles. Series called "C for smarties". It's __very__ good.

http://web.torek.net/torek/c/

The link is in there in the wiki somewhere, but I think it's worth placing it separately.

The wiki mentions some names in the Usenet page. Google these names.


Some thoughts:

>> A pointer is a variable that holds, literally points to, a memory address

What is a memory address?

How does a pointer literally point to a memory address?


C assumes some sort of object space addressable to the byte (each object is composed of one or more bytes -- no such a thing as a 0 bytes object in C). It's the memory pretty much. Afaik, what is an address is not defined as part of the language. I guess people should use the knowledge they got from their "how computers work" classes =D. A pointer holds an address. I think that's as deep as you can get with this as clarifying goes.

You can though, say, to help learners that the address of an object is a value which can be used to indirectly access the object. You could say that it's a value that you can keep with you, to access the object later, indirectly.

And, as any language that I am aware of. Things are usually defined abstractly (in terms of what you want to be true about them). C is not different. You won't find concrete definitions (things may look concretely defined, but they're not). As far as we're concerned, addresses are these things that

- the address-of operator returns

- is help by pointers

- can be indirected

- ...

It could be represented as a character string for all we know.


It's the guide to pointers, not the guide to memory addresses—I think readers are expected to google unfamiliar terms.


I think if I understand memory addresses and assembly language, then I understand pointers. If the guide doesn't explain why pointers are useful and efficient on real hardware, then it's missing the point. Which was my point.


Thanks to everybody for the suggestions. I made some changes to denote addressof and better explain arrays. Also stated that the explicit cast wasn't needed.


A couple more things:

- There's nothing special about uninitialized pointers in C. Any uninitialized variable, whether it's a pointer, int, double, etc., contains unpredictable bytes left over from whatever was previously in that memory location, and should not be referenced. Not sure why this belongs in an article that's specifically about pointers.

- You should proofread the whole thing and fix the typos that a spelling checker won't pick up, e.g., "garage" should be "garbage".


Exactly, as soon as I saw "pointers can be null or uninitialized" my thought was "this is a really confused soul who wants to teach others."

I made my own compilers for living but I find the text very confusing. I'm glad I didn't have to learn anything from this text. I still believe people should learn C from this book:

http://en.wikipedia.org/wiki/The_C_Programming_Language

It's not so big and it's really good written.


> On lines 15-16 we assign our void pointer back to our castptr int pointer. Notice the explicit cast needed.

The explicit cast is not needed in C.


Correct. This is also why you do not need to cast the result of malloc(). An assignment of a pointer to void to another pointer initiates an implicit conversion.


And it is actually somewhat dangerous to cast the return value of malloc. Aside from being redundant it can hide an error on compilers which implement an older version of the standard (not uncommon. Anything pre-C99).

If you forget to include stdlib.h the cast hides the error and malloc will be assume to be a function which returns int. makes for interesting runtime errors.

Never cast the return value of malloc in C, and don't write redundant code. C is not C++.


That is exactly what I was thinking when I started reading the link. But in fact I have found only really minor points.

I still think there is already far too many beginner material on C.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: