Live data from Hacker News

The 5-minute Guide to C Pointers

denniskubes.com

1–10 of 75 posts

Re: The 5-minute Guide to C Pointers

#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 feel interested.

`No, you don't understand,' the Knight said, looking a little vexed. `That's what the name is called. The name really is "The Aged Aged Man."'

`Then I ought to have said "That's what the song is called"?' Alice corrected herself.

`No, you oughtn't: that's quite another thing! The song is called "Ways and Means": but that's only what it's called, you know!'

`Well, what is the song, then?' said Alice, who was by this time completely bewildered.

`I was coming to that,' the Knight said. `The song really is "A-sitting On A Gate": and the tune's my own invention.'

The song that the Knight is referring to is this one: http://en.wikipedia.org/wiki/Haddocks%27_Eyes

Re: The 5-minute Guide to C Pointers

#6
post #3

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

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?

Re: The 5-minute Guide to C Pointers

#7
post #3

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?

Re: The 5-minute Guide to C Pointers

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

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

Re: The 5-minute Guide to C Pointers

#9
post #3

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

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

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

Re: The 5-minute Guide to C Pointers

#10
Every time someone tries offering a simplified explanation of pointers, I've countered with the old Buddhist saying that, "The pointing finger is not the moon," followed by a brief foray into syntax and operators, e.g.,

  moon* finger = &luna;
As often as not, enlightenment occurs.
Post reply on HN