Live data from Hacker News

The 5-minute Guide to C Pointers

denniskubes.com

41–50 of 75 posts

Re: The 5-minute Guide to C Pointers

#41

When printfing pointers you need to cast them to (void\ ). %p only prints void\ and there is no guarantee two pointer types have the same size. And I can't figure out how to write an astrix. I also think it is a mistake to bring up memory addresses so early. The first paragraph is incredibly confuses. A pointer is simple. It's a variable that points at something. You can change where it points and you can change the…

Pedantic notes:

- "The cast isn’t explicitly needed in C but it does make things more readable" - I think many people would disagree with this, including me. Casting is ugly, error prone, and conceals errors. Don't do it, there is no need. If you think it is helping you because you can see what type things are then your functions are too large and incoherent, fix that problem instead.

- "Here we print out the values of our uninitialized and NULL pointers. Notice that our uninitialized pointer has a memory location" - This behaviour is completely undefined and it is doing injustice to those who would benefit from this blog post. All code people run on an example site should be well-defined. "Worst case the program crashes badly" - No, worst case is you have no idea what could happen! Old versions of gcc ran nethack when it detected undefined behaviour at compile time. In practice, crashing is actually the best case since you can see the error. In worst case it silently corrupts data in your program and you can't find it until it's so far from the error site that it's near impossible to track down.

- "using the NULL keyword" - NULL is not a keyword, it's a macro defined in stddef.h (maybe stdlib.h? I forget).

- "doing so will cause a segmentation fault" - No, it's undefined behaviour. On DOS systems the memory addresses 0 was valid and writable.

- "That being said an array variable does point to the memory address of the first element of the array." - No, it IS the first element. It decays to a pointer to its first element under various operations, though.

- "char * fullname = "full name";" - const char * . Modifying string literals is undefined and the fact that the compiler does not require fullname to be a const char * is merely a historic oversight.

I understand the desire to learn-by-writing, but I don't feel this tutorial offers much to the discussion and confuses some of the same things the many other pointer tutorials confuse. I don't think someone new to pointers will actually come out with a superior understanding of pointers after reading this. Much of the content is not technically wrong the wording and method of introduction isn't any less confusing.

Re: The 5-minute Guide to C Pointers

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

Re: The 5-minute Guide to C Pointers

#43
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 had the same opinion at one point. Like you said, the concept of pointers is very simple, but it can get confusing in application, as you start dealing with more levels of indirection. Try coding something/understanding code that requires triple star pointers, then you'll see why they're confusing.

Easy, just grab a pencil and paper, draw the data structures and you're done.

Again, maybe it just shows my age, as I started coding with BASIC and Z80 assembly.

Re: The 5-minute Guide to C Pointers

#45
post #29
post #18

Earlier quoted context omitted.

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

Re: The 5-minute Guide to C Pointers

#48
> 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)

Re: The 5-minute Guide to C Pointers

#49
post #33

Earlier quoted context omitted.

I never really had much of a problem with the syntax, but the point at which the behavior of pointers really clicked for me (after a night of many segfaults of course) was when I realized that when you pass a pointer to a function, you're sending a copy of that pointer, just like with any other primitive type. E.g: void foo(int *p) { /* assignment won't be persistent after foo() returns; need to send **p */ p = (int…

I experienced the same frustration. The exercise that gave me the "ah-ha!" moment I needed was prepending to a linked list via the head instead of the tail: You need to pass the address of `head` into the function so that the new head is reflected in the calling environment when you say `head = np`.

... or have the list operation return the new list head, which can make it way cleaner. See glib's list APIs, for instance.

Re: The 5-minute Guide to C Pointers

#50
post #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…

You might want to look at Go. It always felt like a revised version of C with great standard library and sane defaults.
Post reply on HN