Live data from Hacker News

The 5-minute Guide to C Pointers

denniskubes.com

11–20 of 75 posts

Re: The 5-minute Guide to C Pointers

#11
post #2

30 second guide to C pointers, from Alice in Wonderland : `It's long,' said the Knight, `but very, very beautiful. Everybody that hears me sing it -- either it brings the tears into their eyes, or else -- ' `Or else what?' said Alice, for the Knight had made a sudden pause. `Or else it doesn't, you know. The name of the song is called "Haddocks' Eyes."' `Oh, that's the name of the song, is it?' Alice said, trying to…

Over the past four months I wrote a library in C. In said library exists the following chain:

typedef hidden pointer -> object -> dictionary -> attribute object -> linked list -> object

Then the whole thing can start again from the last object. At both the linked list and dictionary points the pointers have been cast to void. During development there was linked list in place of the dictionary. Since this was before I wrote output debuging involved manually walking the two linked lists.

Else where a function takes a void * array of structs yet needs to access the contents and thus requires a pointer to an accessor function.

I love C but it can get confusing. My plan is to spend the next few weeks in recovery learning RoR.

Re: The 5-minute Guide to C Pointers

#13
> 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.

Nope. Arrays are not pointers; pointers are not arrays. This is perhaps the most common misconception about C, and a "Guide to C Pointers" should not propagate it.

For more information about why this is wrong, read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com).

Re: The 5-minute Guide to C Pointers

#14
post #6

Earlier quoted context omitted.

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

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.

Re: The 5-minute Guide to C Pointers

#15
post #3

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

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

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 =)

Re: The 5-minute Guide to C Pointers

#16
post #3

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

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

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.

Re: The 5-minute Guide to C Pointers

#17
post #7

Earlier quoted context omitted.

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…

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.

Re: The 5-minute Guide to C Pointers

#18
post #7

Earlier quoted context omitted.

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…

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.

Re: The 5-minute Guide to C Pointers

#19
post #15

Earlier quoted context omitted.

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

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.…

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

Re: The 5-minute Guide to C Pointers

#20
post #19
post #15

Earlier quoted context omitted.

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.…

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.

Post reply on HN