Live data from Hacker News

The 5-minute Guide to C Pointers

denniskubes.com

51–60 of 75 posts

Re: The 5-minute Guide to C Pointers

#51
post #3

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

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.

Re: The 5-minute Guide to C Pointers

#52

> The * operator is used to both declare a pointer variable and to dereference a pointer depending on where it appears. You seem to be suggesting that this is an inconsistency, it isn't: int *ptr; means that (*ptr) is of type ptr. *ptr = 3; Means the same and affect 3 to (*ptr)

Don't you mean

  (*ptr)
is of type int?

Re: The 5-minute Guide to C Pointers

#53
post #45
post #29

Earlier quoted context omitted.

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/transistor…

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

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

Re: The 5-minute Guide to C Pointers

#54
The nontechnical guide to C pointers:

They're post office box numbers.

In this analogy, the post office is all your RAM. The big place where my analogy breaks down a bit is that, in this little world, the post office will store things bigger than one box in multiple adjacent boxes, so to get anything into or out of the post office you have to specify which box you want them to start with and how many boxes they'll have to use.

The star (dereference) operator takes as its argument a post office box number and instructs the machine to go to that post office box and start taking things out of enough boxes beginning with that box to satisfy the type of the pointer. The only things that only take one box are char values; the number of boxes everything else takes up depends greatly on the specific kind of hardware.

It's possible to put a slip of paper containing a number into a post office box; in 32-bit x86, a number long enough to encode a post office box number takes up four boxes. In 64-bit x86, it takes up eight boxes.

Using two stars means 'Go to this box, take out enough stuff from the next few boxes to make a pointer, go to the box specified by that pointer, and take enough stuff out from that box (and possibly the next few) to satisfy the type of that pointer.' Using three stars involves another go-to-box step, using four stars another, and using five stars is usually a sign of gross mental derangement.

(Oh, and if you're running under an OS much more featureful than MS-DOS, the post office box is a total lie told to your application by the OS. Getting into that gets a bit complicated.)

Re: The 5-minute Guide to C Pointers

#55
post #42
post #30

Nice tutorial! Maybe it shows my age, but I am yet to understand why so many developers nowadays have such a hard time grasping pointers, regardless of the language being used to teach them.

My first programming language where I used pointers was 68000 and they were easy for me to grasp, but for some reason it was harder when I started using C and everytime I get back to C or C++ I need a little refresher. For me it is the syntax used by C for pointers that makes it confusing.

> For me it is the syntax used by C for pointers that makes it confusing.

Personally I don't see much difference between * (C way), ^ (Pascal way) or ref (Algol way).

Or do you mean the declaration order ?

int* ptr; (right to left)

ptr = ^Integer /ref int ptr (left to right)

Re: The 5-minute Guide to C Pointers

#56
post #46

So... What do you use them for?

There are many uses. It greatly helps to understand the difference between the heap and the stack, and to understand the difference between static and dynamic allocation.

Functions in C are "pass by value" rather than "pass by reference". To simulate "pass by reference", the "value" you would pass is the memory address of the data in question, ie the pointer.

Also, if you put data on the heap, you must keep a reference to its memory address, otherwise you wont be able to access it again. You use a pointer for this. The following bit of code allocates some space on the heap, sticks an integer (5) on it and then returns a reference to that memory address, which is stored in a pointer variable named x.

  int * x = new int(5);
To understand why you would do the above instead of "int x = 5", you really need to understand what the stack and heap are, and why/when to use them.

Also, you can create read-only versions of variables this way. Eg:

  int x = 5;
  const int * y = &x;
At this point, y points to the address containing x, but can only be used to read. printing out "x" and "* y" will both give you the same result. If you want to update it, you can do "x=6", but "* y=6" will cause a compile time error, because of the const.

There are probably loads of other uses too. I only started learning C/C++ last month, in my spare time. Hmm, I seem to have learnt a few things.

Re: The 5-minute Guide to C Pointers

#57
post #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.

void* finger = &luna;

Don't force the finger to only point to the moon.

Re: The 5-minute Guide to C Pointers

#58
post #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.

void* finger = &luna; Don't force the finger to only point to the moon.

[deleted]

Re: The 5-minute Guide to C Pointers

#59
post #39

Pointers are sometimes not arrays. :) extern int *x; extern int y[] x is a pointer to an int and y is a pointer to an array of ints of unspecified size. It's equivalent to saying float x and then extern int x somewhere else. They're type mismatched. If you said 'x is a char pointer', you mean lookup symbol table for address of x, then do a memory address dereference and then get the contents of the memory at derefere…

Arrays and pointers are completely different entities in C. The only reason people think there's any sort of equivalence is because:

1. The "array indexing operator" [] actually works only on pointers.

2. Arrays are automatically converted to a pointer to the first array element whenever necessary.

3. Arrays are really uncommon in C, and many things we think of as arrays are not arrays as C considers them. Declare a function parameter with []? That's a pointer, not an array. Malloc a bunch of memory? Not an array.

Re: The 5-minute Guide to C Pointers

#60
post #30

Nice tutorial! Maybe it shows my age, but I am yet to understand why so many developers nowadays have such a hard time grasping pointers, regardless of the language being used to teach them.

I learned BASIC, then Pascal, then C. I had a terrible time understanding pointers, until one day it clicked, and ever since that point I have a terrible time understanding how anyone could not understand pointers.

I don't think having difficulty understanding pointers is anything new. I was struggling with them in the early 90s, and there was plenty of literature out there talking about how to understand them and how people had trouble with them. But I can't help you understand why people have trouble, because I don't know myself.

Post reply on HN