5 ms·
> pointers are one of those concepts that's easier to use than to explain. My experience is the exact opposite. A pointer is just an address for something. T
by CivBase 6y ago
> pointers are one of those concepts that's easier to use than to explain.
My experience is the exact opposite.
A pointer is just an address for something. There is a "reference" operator to get a variable's address (pointer) and a "dereference" operator to get the data at a given address. That's it.
The hard part for me has always been using them correctly - keeping track of what points to where in my head, especially for void pointers. I haven't done any significant development in C since my early programming years, but that's the part I remember struggling with.
They're sort of like GOTO in that they're easy for me to understand, but they can quickly make my code hard to follow.
- nobleach 6y agoBut then you need to explain to a newcomer that you can have `int ptr;` (pointers to pointers)... It's pretty odd for someone coming from say, Python, Java, or JavaScript. (which, many go through years before realizing that pass by reference happens in their language too)
- CivBase 6y agoIs that really any more complicated than arrays of arrays or structs within structs? Pointers to pointers definitely makes things harder to keep track of, but I don't think they're conceptually difficult.
- MaxBarraclough 6y ago> you need to explain to a newcomer that you can have `int ptr;` (pointers to pointers)... I think HN swallowed the asterisks there. I presume you meant: int **ptr; > It's pretty odd for someone coming from say, Python, Java, or JavaScript. (which, many go through years before realizing that pass by reference happens in their language too) That doesn't sound right. Java always passes by value. https://stackoverflow.com/a/40499/ https://stackoverflow.com/a/40499/
- jagged-chisel 6y ago> For objects, the pass by value is the value of the reference to the object. That's exactly what "pass by reference" means. According the SO definition, C is always pass by value: the value of the 'primitive' (int, long, char) or the value of the pointer (an address.) It pretty much applies to any language: ultimately you're passing a value from the caller to the callee. Whether that value is 'raw' (say, an int) or a pointer or reference is all in how you (or your compiler, or your runtime) interpret that value you received.
- MaxBarraclough 6y ago> That's exactly what "pass by reference" means No, that's wrong. Pass-by-value with references as values, is not the same as pass-by-reference. Plenty of people make the mistake of equating the two. They can get away with the misconception as long as they don't encounter a language like C# that features both reference types and optional pass-by-reference. > According the SO definition, C is always pass by value That's correct. C is always pass-by-value. You can get a similar effect to pass-by-reference, by passing a pointer. > It pretty much applies to any language: ultimately you're passing a value from the caller to the callee. That's incorrect. If the language has pass-by-reference semantics, then it does not copy the value, it passes the callee a reference to the variable. It's not true of pass-by-name, either, but don't know much about that strategy. A good article on this topic is the documentation on C#'s ref keyword, which enables pass-by-reference. The article does a good job explaining the related concepts. [0] The acid test for whether pass-by-reference is happening, rather than pass-by-value, is whether the asserts can fail in code along the lines of this Java code: int myInt = 42; doStuff(myInt); assert(42 == myInt); // Ok String myString = null; doMoreStuff(myString); assert(null == myString); // Ok In Java, the asserts will never fail. The two methods called do not have access to either the myInt local or the myString local, they only have access to copies of the values held in the two locals. This is the reason Java would permit us to use the final modifier when declaring the two locals, without needing to know anything about either of the called methods. On the other hand, C# permits pass-by-reference. Here we'll suppose that both doStuff and doMoreStuff declared their parameters using the ref keyword: int myInt = 42; doStuff(ref myInt); // If callee uses 'ref' keyword, caller must use it too assert(42 == myInt); // May fail string myString = null; doMoreStuff(ref myString); assert(null == myString); // May fail In this case, either assert may fail, depending on the behaviour of doStuff and doMoreStuff. The callees are given references to our locals, and so they may mutate our locals (as well as reading their values of course). Related to this, this is illegal in C#: doStuff(ref 42); // Compile error. 42 is not a variable! [0] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- jasode 6y ago>A pointer is just an address for something. There is a "reference" operator to get a variable's address (pointer) and a "dereference" operator to get the data at a given address. That's it. Everybody's brain is different but your exact "simple" explanation for pointers has been tried on many beginners and they still don't "get it". It's still going to be confusing. For many (most?) beginners, it's only after they've done some examples when they can go back and re-read your simple explanation such that it makes sense. Therefore, your simple "definition" becomes understandable after the person has learned what pointers are through examples.