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.
Being able to do arithmetic on pointers and be able to arbitrarily dereference them is what makes them pointers. Other languages have references, which almost certainly end up being implemented with a pointer somewhere underneath the hood, but that doesn't make them pointers.
Incorrect. People still call them “pointers” even if you can’t dereference them at all (like void*). People also still call them pointers even if you can’t do arithmetic on them (like function pointers).
That's a good point, but I still disagree since, in the end, those pointer types can always be cast to at least one different pointer type (char *) that you can do pointer arithmetic and do arbitrary dereferences on. Reference semantics ultimately do have entirely different semantics from pointers, so they are different.
“Reference semantics” and “pointer semantics” are kind of vague terms. The terms are used differently in different languages. C has pointers in the language standard but “references” is not a technical term in C. C++ has a technical notion of references but it is different from the semantics for references in other languages.
tl;dr within the type hierarchy, pointers and reference types are subtypes of abstract references, making them distinct types but equivalent as references.
Function pointers are a unique kind of referencepointer type that can't be substituted with object pointers in either direction, so they're an entirely distinct type. added: The problem is that C defines pointer in such a way that it's just a distinct object which references another object or function, which is pretty vague, so the actual type semantics depend on the derivation. In essence, C doesn't have a pointer type, but it has object pointer types and function pointer types, which are loosely grouped as pointer types under the standard, with object pointers often simply being called pointers.
void*, meanwhile, is just a supertype of object pointers with restrictions implied by the language semantics (arithmetic and access can't be reasoned about without a size). It's effectively an "abstract object pointer" in how it's defined by the standard.
edit: I suppose the best way to actually summarize the above is that C has "object pointers" and "function pointers" as distinct concrete pointer types, with the only notion of an abstract pointer overall being "it's an object which refers to something". Added and removed words in the above to make this more clear, and removed sentences entirely that clouded this. /edit
This directly follows through to your original Python example. The reference types in Python and similar languages aren't pointers. There's nothing about the types themselves that makes them pointers; they behave like pointers because both reference types and pointers are subtypes of abstract references, but the concrete types in use in C and Python are distinct from one another. This shines through most in C#, where both pointers and reference types are available, which makes the type distinction obvious.
Sure. But they are still all called “pointers” in C.
“Pointer” is just a name people use. It doesn’t have a rigorous definition which everyone agrees on. It just has various contextual uses that share a lot of similarities—like, when a C programmer talks about pointers, that includes function pointers and void pointers. When a C# programmer talks about pointers, they’re talking about things like “unmanaged pointers” which is a meaningless term in most languages.
I get where you're coming from, but I think that's why using reference instead of pointer makes more sense as the generic term. An abstract reference is more well-defined by theory to be anything which references something else, whereas pointer can be a bit wishy-washy with expectations.
I can see how someone could take "a pointer points at something" and interpret it as being 1:1 with an abstract reference, but other people associate it with a distinct value that can be interacted with or a form of direct reference.
I'll admit I haven't thought about people interpreting pointer this abstractly before, but I do feel reference better serves the purpose when used in this way to avoid confusion.
This is kind of an issue with pedagogy, where you try to find the best word for teaching students a concept that has the least probability of confusion and the maximum probability of understanding.
I'll admit I haven't thought about people interpreting pointer this abstractly before, but I do feel reference better serves the purpose when used in this way to avoid confusion.
There’s a pedagogical purpose to saying “Python reference types are pointers” and that’s to get people to think about the ways in which that statement is true. The purpose is not to tell people something like “Python reference types and C pointers are exactly the same”. I hope people are smart enough to understand that they’re not exactly the same, but that there is a lot that they have in common.
In particular, a Python object in CPython has a memory location, a variable which refers to an object contains that object’s memory address. If you know how pointers work, then you know something about the way Python works. Even if it doesn’t suddenly unlock a bunch of new understanding, it helps you make connections between the programming practices you have in C and the ones you have in Python, and it helps you ask interesting questions about Python semantics (like why += modifies a list in-place, but doesn’t modify an integer in-place).
The catch here with terminology is that terminology only gets you so far—you can have a long discussion about whether “pointer” or “reference” is the right word for something, but once you get to a certain level of detail, you need to discuss things in more concrete, specific terms.
"Pointer" is a lexeme of all programming languages. Like "instantiate," "reference," "object," or "value." It is the object holding your reference (usually to memory). It has a size. I can set other variables equal to it. I can pass it in functions. I can operate on the pointer, with or without de-referencing said pointer.
It doesn't matter what the language calls them. They are simply rebranding a pointer.
The python interpreter uses pointers all the time. A list or an array in python? That's just pointers to a list of pointers. Python saves you that trouble. It also denies you the power that comes with controlling the granularity. C, does not. C gives you live ammo, and trusts you to not shoot yourself.
It's not even that it trusts you. It trusts you no more than a rock and a high cliff trusts you. The makers never cared what you would do with it. You can shoot yourself all day. You want a safety? Make one with C and make a library that you reference. You'll want to use "pointers" in that solution; they make for cheap data integrity.
What does the python interpreter use to pass objects that hold memory references? And that name, it may be a label, that references a computer science term. What term is that?
The language itself is not the same as its implementation; rather, it is defined by its specification. Otherwise, C wouldn't "exist" without a compiler either!
You should read the Reference page instead. Pointers are only one kind of reference, and the underlying implementation of a language has no impact on what its type system actually conveys.
If you aren't directly interacting with a pointer type, you aren't using a pointer; you're using a reference which may or may not act as an abstraction over a pointer within the implementation.
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.