r/C_Programming • • Apr 24 '24

The way C Programers explain pointers

https://imgs.xkcd.com/comics/pointers.png
652 Upvotes

50 comments sorted by

View all comments

30

u/EpochVanquisher Apr 24 '24

There are pointers in most languages.

The difference is that C calls them “pointers”, makes you manage lifetime manually, and lets you do arithmetic on pointers. But the hard part about learning to use pointers is not any of the things unique to C; instead, it’s understanding conceptually how object references work; it’s understanding that two different references can refer to the same object.

It’s one of the most common gotchas that trips up beginners in Python, for example.

1

u/Jarhyn Apr 24 '24

Everyone, when starting to learn pointers, should do the bucket exercise.

The bucket exercise is when you and a couple buddies have a bunch of numbered buckets.

The buckets are the "physical addresses", and their contents are the "data at address". Buckets can contain only values that represent other buckets.

Then you can take turns asking for the "literal value in the bucket", the "indirected" value in the bucket, and "the address of the bucket".

After this exercise you should hopefully be able to concretely distinguish between the idea of "the contents of the bucket", "the identity of the bucket", "the identity the contents of the bucket's reference", and "the contents of the bucket referred to by the bucket's reference" and so on.