News for hackers. Stuff that matters.
Re:C for Python programmers (2011) by protomikron on Monday November 07, 2016 @04:33PM (#12895359)
IMHO pointers are mostly hard at the beginning because the asterisk-operator is overloaded: On one hand it declares (and optionally initializes) a pointer (type * x = ...;) and on the other it dereferences it (type y = * x;). At least for me that was the thing that puzzled me the most. I would prefer something like that:

  // declare and initialize x
  @uint32_t x = 42;

  // dereference x
  uint32_t y = ~x;
I.e. have different symbols for the type and the dereferencer ('@' and '~' as an example).

//edit: thanks for the help, fixed snippets - HN's parser does not get pointers either apparently :)

Re:C for Python programmers (2011) by roel_v on Monday November 07, 2016 @07:47PM (#12896670)
I've been programming C++ for too long to really understand why people find pointers hard, but I do remember that I did too at the beginning, and I do remember my epiphany (reading on the train, funny how I remember such senseless details 15 years later yet can't memorize a 4-item shopping list): given a declaration of

    struct Foo {
        int a;
    };
    Foo* f;
, the following two are equivalent:

    int b = f->a;
and

    int b = (*f).a;
Never had it spelled out for me like this, probably because it's so obvious once you get it - thought I'd share in case it gives anyone else an 'aha' moment.
Re:C for Python programmers (2011) by acbart on Monday November 07, 2016 @08:52PM (#12896974)
The trick to teaching pointers is to model memory with boxes. You have a stack, and a heap, and you draw arrows to represent pointers. At some point, you need to reveal that those arrows are just numeric indices, but the visual helps early on. The * and & operators just let you follow the value in the pointer. I've had a number of students who were baffled up until I showed them this model.

Notational Machine for the win!

Re:C for Python programmers (2011) by stevekemp on Tuesday November 08, 2016 @12:02AM (#12897796)
Back in the 80s the programming books I used always showed memory as little lines of boxes.

https://usborne.com/browse-books/features/computer-and-codin...

Re:C for Python programmers (2011) by userbinator on Monday November 07, 2016 @09:51PM (#12897209)
In general, thinking of memory as an array of bytes is helpful, and each byte has an address which is itself a series of bits that can be stored in memory --- and the concept that ties it all together and what I find makes a lot of people just "get it" is the fact that memory can itself store addresses that denote locations within it.
Re:C for Python programmers (2011) by Anthony-G on Tuesday November 08, 2016 @04:27PM (#12904092)
That's a perfect example of how knowing some assembly code helps with learning C.
Re:C for Python programmers (2011) by allenbrunson on Monday November 07, 2016 @08:06PM (#12896769)
i also had trouble with pointers while learning C, and, like everyone else, it seems so obvious now that i wonder what the hell my problem was. like learning to ride a bike, or drive a car with a manual transmission. i learned 8086 assembler before C and had no difficulty with indirection there.

for what its worth, your "aha" scenario would have just confused me more. even today, i have to think a bit before your second example makes sense to me.

Re:C for Python programmers (2011) by emmelaich on Monday November 07, 2016 @06:27PM (#12896232)
You can consider them the "same" thing if you regard the asterisk as binding closer to the var instead of the type.

Which the declaration syntax hints at:

   int i, *p;
Re:C for Python programmers (2011) by protomikron on Monday November 07, 2016 @07:01PM (#12896435)
Can you elaborate?

My point is that the concepts "having a datatype, that stores an address" and "reading (typed) data stored at a specific address" are different, but share the same keyword (which is in this case just one symbol - maybe because C programmers have to use it often).

E.g. assuming C99's bool (0|1) datatype, you might define the "negate" functionality, which is often done by '!'. So we have something like that:

  // init a new bool
  bool success = 1;
  ...
  // negate the bool
  if (!success) {
    ...
  }
For me that makes sense. However if we take the pointer approach (as it is implemented), it would look like this:

  // init a new bool
  bool success = 1;
  ...
  // negate the bool
  if (bool success) {
    ...
  }
I hope this makes sense. For an experienced programmer * 's role is obvious from the context, but for me that was the most troubling concept when starting with C's pointers.
Re:C for Python programmers (2011) by munificent on Monday November 07, 2016 @07:14PM (#12896506)
> My point is that the concepts "having a datatype, that stores an address" and "reading (typed) data stored at a specific address"

They are different things, but C's syntax was deliberately designed to imply the former from the latter. "Declaration reflects use" is the term they used for it.

The idea is that in something like:

    int *i;
You don't read it like, "Declare a variable 'i' whose type is 'int star', which is a pointer to an int". You read it like "declare variable 'i' whose type is such that taking 'star i' (i.e. doing a dereference) would give you an int". The type in this case is a pointer to an int.

This is always why function pointer syntax is so totally bizarre in C.

"Declaration reflects use" was a neat idea, but I think it practice it ended up causing more confusion than it solved. At the time, maybe they thought users would be tripped up by compound type expressions and thought it would help if they focused on the operations performed on the variable being declared.

In practice, it turns out that composed types don't seem to be that hard.

Re:C for Python programmers (2011) by protomikron on Monday November 07, 2016 @07:39PM (#12896628)
Wow this is really interesting, I did not know that, and it explains their choices. And C's function pointer types are indeed written in a bizarre way.

I definitely think of (int * i) as (i :: Ptr Integer) and (* i) in an expression like 42 + * i as (* :: Ptr Integer -> Integer).

Re:C for Python programmers (2011) by evincarofautumn on Monday November 07, 2016 @10:15PM (#12897311)
Yeah, as long as we’re mixing notations, it’s really:

    *i :: Integer
From which you can conclude i :: Ptr Integer.

And the dereference operator does have the type you specified, but not in an lvalue—there the operator is really a mixfix one:

    (*_ = _) :: Ptr a -> a -> a
This isn’t specified directly by the standard, but follows from the rules about how an assignment operator must examine the structure of its first operand.
Re:C for Python programmers (2011) by yarou on Monday November 07, 2016 @07:41PM (#12896640)
> In practice, it turns out that composed types don't seem to be that hard.

Depends on what you mean by hard. Hard to read or hard to reason about?

* -> * and * -> * -> * are easy to read and reason about, i.e. Int -> Char -> Int. But what about types of types?

(* -> * ) -> * is confusing to reason about IMO. i.e. Applicative Functors:

(<$>) :: Functor f => (a -> b) -> f a -> f b

Not really intuitive at all, the only reason I can understand it is that I know what functors and Monads are.

Re:C for Python programmers (2011) by munificent on Tuesday November 08, 2016 @05:34PM (#12904560)
I'm not talking about higher-kinded types, just type annotations that are more than just a single bare identifier. Stuff like:

    List<int>
    (Some, Tuple, Type)
    Matrix[4][4]
    (Para, Meter) -> ReturnType
Re:C for Python programmers (2011) by roel_v on Monday November 07, 2016 @07:52PM (#12896696)
I'm probably misunderstanding you, but can I rephrase your comment as saying 'int i is the natural way of declaring a pointer to an int, and not int i'? Because if so, I don't recall that from the K&R (and I've read it front to back several times, not that that means much - I can't even remember if they use int* i or int *i, and I don't have my copy at hand here).
Re:C for Python programmers (2011) by munificent on Tuesday November 08, 2016 @05:38PM (#12904594)
Alas, HN mangled your asterisks.

> I don't recall that from the K&R

I don't recall either, but this Wikipedia mention of "declaration reflects use" cites K&R:

https://en.wikipedia.org/wiki/C_%28programming_language%29#C...

Re:C for Python programmers (2011) by roel_v on Tuesday November 08, 2016 @06:13PM (#12904811)
Uh sorry, the first one had the asterisk next to the variable name, the other next to the type.

Yes I agree about the 'declaration reflects use'; what I meant was: does that imply that the declaration should consider the asterisk (the 'make this a pointer' part) to be 'part of', and thus right next to, the variable name or the type?

I'm strongly in the 'type' camp myself, and therefore I think that int *p; is nonsense; only to be used out of necessity when declaring multiple pointer variables on one line. So I'm wondering if 'declaration reflects use' reaffirms that, or contradicts it.

Re:C for Python programmers (2011) by belovedeagle on Monday November 07, 2016 @07:12PM (#12896495)
I don't see how the bool example follows; in what way is negating a boolean like dereferencing a pointer w/r/t types? ! is bool->bool; * is * P -> P.

The reason the same symbol is used is because of the way C types are read:

    int x; // (x) is an int
    int *x; // (*x) is an int
    int **x; // (**x) is an int
    int x(double); // (x(1.)) is an int
Re:C for Python programmers (2011) by protomikron on Monday November 07, 2016 @07:32PM (#12896597)
Yeah, sorry, my example sucks.

However, I am still believing a different operator would make more sense.

Re:C for Python programmers (2011) by belovedeagle on Tuesday November 08, 2016 @12:12AM (#12897848)
And what of [a-zA-Z0-9_]? Why should we allow them in both types and variables and functions? That's really confusing.
Re:C for Python programmers (2011) by emmelaich on Monday November 07, 2016 @10:29PM (#12897375)
My elaboration is a bit late ... but.

    int i, *p;
Tells me that i is an int. And so is *p.
Re:C for Python programmers (2011) by metaphor on Tuesday November 08, 2016 @03:51AM (#12898646)
This had a way of always confusing me. For clarity, I'd declare them as:

  int i;
  int* p;
Re:C for Python programmers (2011) by mfukar on Tuesday November 08, 2016 @04:40AM (#12898885)
Think of it from the perspective of types:

    int *p; /* What is the type of p? Pointer to int */
    int a = *p; /* What is the type of *p? int */
Re:C for Python programmers (2011) by mixmastamyk on Monday November 07, 2016 @05:51PM (#12895980)
I first learned pointers with Pascal and believe it was simpler with the ^ operator, but it has been so long I don't remember.
Re:C for Python programmers (2011) by e12e on Monday November 07, 2016 @07:18PM (#12896530)
For some reason I also found pointer/references easy in Pascal, but struggle with them in C. I also find it easier to work with direct/indirect addressing in assembler than in C. I'm not entirely sure why - seemingly the differences between C and Pascal syntax and behavior for references seem trivial -- and yet somehow in Pascal it feels intuitive, while in C it feels complicated.
Re:C for Python programmers (2011) by Iv on Monday November 07, 2016 @10:25PM (#12897357)
If I recall correctly (long long time ago) there was only one pointer type un Pascal and it was considered a number that it was legal to modify without warnings.

C insists that int * , char * and int are totally different types that you should not mix. It makes sense most of the time but it can be confusing when you do not realize that internally they typically are the same thing <insert here the disclaimer about 32/64 bits systems>.

Re:C for Python programmers (2011) by evincarofautumn on Monday November 07, 2016 @10:26PM (#12897362)
I think it seems simpler because Pascal differentiates the type ^integer (“pointer to integer”) from the expression i^ (“dereference i”) and didn’t originally have an address-of operator—you could only get the address of a new anonymous value.
Re:C for Python programmers (2011) by e12e on Tuesday November 08, 2016 @02:20AM (#12898325)
It's been so long, I don't really recall - but I was using object pascal which is fairly similar to free pascal:

http://www.freepascal.org/docs-html/ref/refse15.html

It's quite similar to c - yet I find it a little bit clearer.

Re:C for Python programmers (2011) by mettamage on Tuesday November 08, 2016 @12:12AM (#12897847)
For me the reason pointers were hard is because I didn't get clear feedback when I dereferenced them. When pointers are taught conceptually they don't tell you that there is a kernel terminating the program because you access invalid memory.

I confused that termination with the idea that my pointer has never been dereferenced in the first place. Now, I don't know if the kernel intervenes before the invalid address is going to be dereferenced or just after it. The thing is, I understood the concept, but getting this error was giving me doubt since I made an error of accessing valid memory.

Once I understood that I tried to force a normal int to become a pointer and eventually it worked (needed two ints and an 8-bit variable to cast it to a 64 bit address), proving to me that I understood the concept.

Accessing (valid) memory and pointers should be explained together.

Re:C for Python programmers (2011) by caro_douglos on Tuesday November 08, 2016 @01:12AM (#12898071)
I recall them being a way of seeing how much you studied at uni...or some professor wanting to bring up the morris worm
Re:C for Python programmers (2011) by brandmeyer on Tuesday November 08, 2016 @12:45PM (#12902310)
You may be interested in the SPECS design. They used a prefix ^ for this purpose.

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.34....

Re:C for Python programmers (2011) by jpindar on Monday November 07, 2016 @05:10PM (#12895682)
I agree, the notation was the hardest part of learning about pointers. Having a 'cheat sheet' - a program with examples of common ways of using pointers - helped me quite a bit.
Re:C for Python programmers (2011) by blueside on Monday November 07, 2016 @09:46PM (#12897189)
I well remember that if how arrays decay to pointers was properly explained to me in the beginning, I would have "got" pointers a lot sooner
Re:C for Python programmers (2011) by grahamburger on Monday November 07, 2016 @08:12PM (#12896797)
This exactly captures the struggle I've been having with pointers in Go - I have no problem grasping what a pointer is and how to use it, but I keep getting tripped up on the syntax because of exactly this.
Re:C for Python programmers (2011) by greenshackle2 on Monday November 07, 2016 @04:37PM (#12895401)
I was about to tell you to escape it but I actually cant find a way to print an asterisk on HN

EDIT: you can print one by leaving a space after it: * aa

Re:C for Python programmers (2011) by protomikron on Monday November 07, 2016 @04:43PM (#12895453)
Yeah thx, I just realized that myself.
Re:C for Python programmers (2011) by jstimpfle on Monday November 07, 2016 @04:38PM (#12895403)
Do it like * this