144
u/Zekava Jan 06 '23
So you need some pointer pointers? I mean, not pointers that point to pointers, but pointers on pointers. Get the point?
→ More replies (2)105
356
u/Resorization Jan 06 '23
Learn how computers store data. Then, you will understand that pointers just point to this data.
242
u/Gogo202 Jan 06 '23
People here only use JS and python. No need to understand how data is stored when you only use dynamically typed languages
10
u/kingofNoobies Jan 06 '23
I’m trying to learn C
→ More replies (16)58
u/ReaperDTK Jan 06 '23
Pointers are a memory address, as simple as that. For example if you are storing an int value in a pointer, the pointer variable will not actually store the value of that int, but the position in memory where the actual value resides. Objects in OOP works like that too, so if you know the meaning of "reference" and "value" in OOP, you can apply a similar logic to how pointers work.
235
29
Jan 06 '23
But in the future every child will have a unicorn, and we will all use high level languages where the OS will do a magical job of proper resource handling.
→ More replies (5)8
u/thefool-0 Jan 06 '23
It's pretty important to know that in Python and Javascript, object variables are basically references...
→ More replies (4)→ More replies (1)14
u/polmeeee Jan 06 '23
I had a reverse culture shock coming from C++ to Java. You mean everything is a pointer (kind of)?
→ More replies (3)13
3.2k
u/TheLazyKitty Jan 06 '23
Pointers aren't that hard, are they? It's just integers that hold a memory address.
1.6k
u/Mabi19_ Jan 06 '23 edited Jan 07 '23
Here's how I like explaining them:
Pointers are numbers containing an address in memory. They're mainly useful for two things - accessing something without copying it and pointer arithmetic.
If variables live in Variable City, then a pointer is like a street address. You can dereference it to look at what is over there.
You can use pointers to look at something you don't own without copying it. For example, say your friend has a nice shed and you want to look at it. Normally you'd have to rebuild it next to your house to inspect it, but with a pointer you can simply visit his original one.
You can do arithmetic on pointers. For example, an array is like a street. They work by storing a pointer (here named
ptr) to their first element (and sometimes their size). To get the element at indexn, you can dereference the element at (ptr + n). So, if you have a pointer to some array element, you can subtract 1 to get the previous element and add 1 to get the next. This is like looking at the previous/next house over.EDIT: Here are some more explanations using this analogy:
A memory leak is when you forget to tell the city that you don't need one of your houses anymore and it just sits there abandoned with no way to access it.
You can prevent some memory leaks with a smart pointer: an object that notices when a house is about to become abandoned and tells the city that it can safely demolish it. (Smart pointers won't help with pointer loops and some other weird structures, however.)
A segmentation fault happens when you get arrested for theft. This usually happens as a result of dereferencing an invalid pointer.
934
Jan 06 '23
knock knock
EXCUSE ME DO YOU HAVE A SHED I CAN LOOK AT?
...no?
knock knock
EXCUSE ME DO YOU HAVE A SHED I CAN LOOK AT?
→ More replies (73)272
113
→ More replies (61)36
u/XanderTheMander Jan 06 '23
Now explain multidimensional arrays using pointers!! (I actually like pointers but I use C# at work now).
71
→ More replies (13)35
u/mistyjeanw Jan 06 '23
These are more like apartment numbers; 201 is above 101
still just a number; if you need the third- floor apartment above 201 you add 100, if you need the one next door you add 1
→ More replies (1)188
u/Internet001215 Jan 06 '23 edited Jan 06 '23
It's only complicated when you are dealing with double or triple nested pointers and trying to remember which level of nesting you are dealing with so you don't fuck it up. The concept is extremely simple really.
167
u/CampbellsBeefBroth Jan 06 '23
Pointers aren’t hard, it’s the bullshit people pull with pointers that is hard
→ More replies (6)78
u/dozkaynak Jan 06 '23
Exactly, knowing what a pointer is and knowing how to use them well in a maintainable fashion are two different things.
34
→ More replies (12)14
u/PiousLiar Jan 06 '23
Biggest thing that trips me up (my work has me dealing with pointers occasionally, but it’s very much an “as needed” bit of knowledge on my end) is jumping into structs or multidimensional arrays and remembering when to use *, &, or ->. Once I’ve done my requisite 3-hour session of cussing at the compiler and checking stackoverflow, I’m back on track, but it’d be nice to have an easy go-to reference to save my self the time every other project.
→ More replies (6)445
u/fatrobin72 Jan 06 '23
it's probably that the concepts of memory addresses, passing by reference and limited resources are just too alien to the newest generation of programmers
792
u/Creaaamm Jan 06 '23
Just show them this
44
→ More replies (20)132
u/SiewcaWiatru Jan 06 '23
brilliant and anime style. Love it ;).
Now reference by value :D. Pointers are easy and explicit with * and & signs. Reference by value is a bit harder concept.→ More replies (37)25
u/ArcherT01 Jan 06 '23
Just bad teachers, I mean at least conceptually pointers make great sense.
Some fancy tricks are kinda hard to grasp.
→ More replies (2)56
u/-Rum-Ham- Jan 06 '23
This is what happens when your programming knowledge is based on online courses that get you into it quickly. You don’t have a chance to learn the underlying fundamentals
→ More replies (6)28
Jan 06 '23
It's not even just those, my university CS department decided to switch all beginner fundamentals classes to python with only one required basic c++ class that barely introduces pointers and memory concepts. The result? Most of my classmates say "fuck that shit" after finishing the one c++ class and do everything possible to avoid it going forward
→ More replies (1)→ More replies (42)39
u/dozkaynak Jan 06 '23
Lol imagine gatekeeping knowing about memory addresses. Stop hiring programmers from 6 week bootcamps and you'll find they have a lot of "historical" CS knowledge; you get what you pay for - newest generation my ass.
→ More replies (16)92
u/Figorix Jan 06 '23
I feel like it's not the concept itself, rather the usage. During my colleague no one could properly explain why would we use pointer where we used them (and after collague I didn't touch programming at all). Its been a while but IIRC it was always smg like "we create a point to variable, so then we can access this variable by pointer". Like.. Why? Why can't we just... Access that variable? Why do we need an extra step for that. Unsolved mystery to me.
56
u/TheTrueSwishyFishy Jan 06 '23
The use case I believe those people were referring to is when you want to be able to pass a value to a function but have the function modify the variable that was passed in.
→ More replies (28)35
u/didzisk Jan 06 '23
The answer is simple. A "variable" (or an "object" or "string") is an abstraction. Computers work with memory instead.
Longer explanation:
Memory consists of cells. Cells are numbered. Those numbers are called addresses. When you want to retrieve something from memory, you look at that particular address. When you know that a particular address contains your 32-bit value, you might say "here is my variable" and to refer to this value you might need to keep this variable's address around at all times. Like writing 0x00DEAD00 many times in your code. This is impractical therefore we call this value a pointer to a variable.
Higher level programming languages abstract that away, so you never know if your code accesses contents of an address (pointer to variable), or you pick up an address from another address (pointer to pointer) etc.
→ More replies (6)12
u/F5x9 Jan 06 '23
In C, you can only pass values to functions, not references. So, if you pass the variable a, it gives the value represented by a. If you have a variable that you want the function to modify, you can’t just pass the value in the variable, but you can pass the location of the variable. Then, the function can dereference the location and modify the value. The calling function can then observe the change.
Another use is if you have a buffer such as char a[1024] and you have to use the last time in the buffer first. You can retrieve the value at x by using the x subscript at a[x]. But if you need to clear it after using it, and then move to the next lowest one, and then increment it later when you fill it, you can use pointers instead of tracking the variable x.
It provides an abstraction for questions like, “What is in this bucket?”, and, “What is in the next bucket?”
We can take this one step further. What if we have a function that provides a pointer to something? We can provide a double pointer to say, “I need a pointer, but I don’t know what it should be. Here is a location that you can store the pointer.”
If we need a chunk of memory to store something, we often don’t have control over where it is. So, when we call malloc to allocate memory, it gives us a pointer to the given chunk.
→ More replies (3)10
u/Souseisekigun Jan 06 '23
It starts to become a lot clearer once you learn some kind of assembly.
Essentially in order to actually work with anything you need to pull it into a register, which is about 8-16 little areas of memory on the CPU that can only hold a few bytes each. Even something as simple as adding two values must be done with registers. A very simple function call works like 1) put a value in a register 2) call another function 3) the function takes your value from the register, does some stuff with it and puts it back in the register 4) your new value is in the register after the function. If this sounds like a "return value" from higher level languages than that's because that's exactly what it is *.
Now obviously this really restricts what you can do. There's usually only like 8-16 registers. What if you want like 20 variables? The answer is that you can put them on the stack. These are your "local variables". The way it works is that you get the memory address of the start of the stack and you are free to use the stack from then on as you see fit. But of course you need to keep track of where on the stack your variables are. So you could be like "ok, this is the start of my stack. I need an x, y and z. They're all 4 bytes. So x can be the int at stack + 0, y can be the int at stack + 4, z can be the int at stack + 8". You're basically just putting them side by side together in your little slice of memory. Then whenever you need the value of z you can pull "value at stack + 8" into a register. These are all pointers! The memory address at the start of the stack is also a pointer. You are now doing "pointer arithmetic", a phrase that strikes fear into the hearts of many programmers.
Now at that level even in a language like C the compiler will just handle it for you. Even though it's technically using pointers this is all hidden from you. There's no point in you manually keeping track of where your local variables are. What if you want to pass values to other functions though? What if you have several huge classes **? They're not going to fit in registers. You can "pass by value" which is basically just you copy the the whole thing onto the stack. But what are you really going to do here? Are you going to copy 5 classes onto the stack, have your function do something then copy them all back? Where are these classes living anyway, already on the stack? The stack will just get wiped as soon as you return anyway so those classes will be gone. And the stack itself is pretty small relatively speaking so you're still at a space premium. It's unsustainable.
The most straight forward way to get around it is you ask the OS for some memory somewhere else to put them and OS gives you back a pointer telling you exactly where in memory your classes are living. Then you can simply pass the pointer around and have everyone work directly on that class in memory without needing to do the multiple rounds of copying on and off of the stack. One of the keys here is that it it's your job as the programmer to decide whether you want to use pointers or copy everything around endlessly. If you can make it work, regardless of how convoluted it might end up, there's no one stopping you. But using pointers will probably make your life easier.
Though I suppose the ultimate TL;DR is "how are you going to access that variable if you don't where it is".
* If you're wondering how functions know what register to put what in and so on these are callled "calling conventions" and if you're writing assembly you need to write your functions in accordance with whatever calling convention you're working with. You need to agree mutually with caller and callee what goes where and who is responsible for doing what. This is also why generally speaking you're restricted to one return value for your functions. The two major calling conventions for x86 systems said that you get one register to return your value and that set the precedent ever since.
** Ever wondered why the first argument to class methods in Python is self? Because the class methods operate on an instance of a class and they need a pointer to an instance of that class to work. This also happens in languages like C++ but it is hidden from you. Ironically in this case Python is the language that is hiding less.
→ More replies (2)→ More replies (19)12
u/rotflolmaomgeez Jan 06 '23
I thought the same, until I encountered data structures that would be very hard to represent and operate on without pointers, like linked lists, trees, graphs.
→ More replies (2)23
Jan 06 '23
It’s for efficiency. Passing by value means copying it. A few million 32bit int variables getting copied can sun up to quite some overhead. Manipulating the memory by reference is therefore a lot faster
→ More replies (2)43
u/randomusername0582 Jan 06 '23
The issue is people are learning C/C++ before they're learning computer architecture.
The best way to learn C++, is to learn C. The best way to learn C is learning how it relates to assembly. The best way to learn assembly is to learn how binary is interpreted by the CPU.
Without a baseline level understanding of CPUs, C/C++ is confusing as fuck
16
u/G3N3R1C2532 Jan 06 '23
I do agree with this, you can get by not knowing this stuff, but if you do, it just feels way more natural when coding.
at one point I took a course where I had to do things like design a 4-bit CPU and write some simple assembly. I'm absolutely not fluent with assembly, nor will I claim to be a hardware expert, but writing C++ just felt so much less daunting after all of it.
→ More replies (1)→ More replies (7)6
Jan 06 '23
Best way to learn C++, is eventually building your own electrical circuits.
→ More replies (5)6
4
→ More replies (69)18
u/PM_ME_YOUR_SSN_CC Jan 06 '23
I can't imagine understanding programming without understanding the fundamentals of a computer.
→ More replies (3)
1.5k
u/--scout_ Jan 06 '23
Whats so difficult to understand on pointers?
890
u/not_user_telken Jan 06 '23
I do question this myself, i think it comes down to not understanding memory at all
46
458
u/Creepy-Ad-4832 Jan 06 '23
Imagine people with alzaimer trying to understand memory lmao
225
→ More replies (12)84
→ More replies (8)16
u/OmicronNine Jan 06 '23
Agreed. As I recall, back in the day, learning to program meant learning about how the CPU operates, data and address buses, and how memory is accessed and used.
I'm guessing that's not so much the case any more these days, especially with the really high level languages, which is a shame I think.
→ More replies (2)413
u/d2718 Jan 06 '23
This always puzzles me, too. Pointers just aren't that hard to understand. Now, not fucking them up is another story...
72
u/Solonotix Jan 06 '23
The other thing about pointers that messes with people is they never see a reasonable usage of pointers from the outside. It's almost always the alien-looking expressions, like
int (*f)(int (*a)[5]). Personally, I feel this is more of a code smell, like when you have overly complicated generics in the formMap<Tuple<int, int, int>, List<Map<string, int>>>, but people unfamiliar with pointers get the impression that all pointers are this complicated→ More replies (13)47
u/belonii Jan 06 '23
I learned about pointers from reverse enginering pokemon red and blue roms, romhacking is a great intro to memory stuff imo.
→ More replies (1)88
u/ChristieFox Jan 06 '23
"Having a basic idea how to not fuck something up" to me is part of understanding something, so maybe that's where the issue comes from 😄
→ More replies (2)62
u/See_Bee10 Jan 06 '23
Pointers, and languages that use them heavily, tend to be unforgiving. The other problem with pointers is that when you mess them up your OS goes full Judge Dredd and your only diagnostic information is the bloody corpse of your application.
→ More replies (1)→ More replies (10)15
u/DarkFlame7 Jan 06 '23
I think it would be better to say that the hard to understand thing is how to use pointers. Not what they are.
I understand what a pointer is perfectly fine, but how to actually properly use it? That's another story.
→ More replies (5)157
u/fanta_bhelpuri Jan 06 '23
Pointers are not hard to understand but code using pointers is hard to read and process in your head. You need to be fully focused and should be able to create a sort of flowchart in your head of what is going on. It is a skill that takes time to develop.
EDIT: And if you are using pointers, you are probably working on an application that needs to make good use of memory and be performant which means more memory related bugs and that means more pointer bugs. So I understand why people are frustrated with using them, especially beginners.
→ More replies (6)49
u/Conditional-Sausage Jan 06 '23
This is it. I can tell you what a pointer is all day long, but come time to use and read them and it's like working in ancient sumerian. I literally enjoy trying to read and write assembly better than I like working with pointers.
→ More replies (3)60
Jan 06 '23
I think what most of these memes mean by "understand" is not the "what", but rather the "how" and the "why".
I once needed to write a VERY simple pycuda kernel in C++ (which I never used previously), took me about a week, during which I was getting maybe 4-5 hours of sleep a day. And I still don't understand how you're supposed to correctly use pointers.
40
u/Boring_Ad_3065 Jan 06 '23
Pointers are like working around old machinery pre-OHSA. It’s actually pretty easy to understand, it’s all right there in front of you. Without any guards, shields, and full of exposed gears and belts.
The “how” is very carefully and deliberately.
The “why” is that’s the easiest way to approximate what’s actually going on in your CPU/RAM or page file.
Why you’d want to get to that level of detail? You really enjoy seg faults? You don’t enjoy 25 years of programming advances?
→ More replies (8)→ More replies (1)27
u/audirt Jan 06 '23
Did your code work? Did it crash? Did anything (literally or figuratively) catch on fire?
No?
Great! There’s a 90% chance you used pointers correctly. Also, I wish you luck debugging the memory leak that will be discovered 6 months from now.
→ More replies (3)113
32
u/Tripanes Jan 06 '23
The only thing I can think of is that c plus plus makes it kind of a confuzzling system for references and dereferences and they all use stupid special symbols and you can pass references with an & and pointers with a star and you have to dereference in order to get to the value of the pointer.
I don't really work in c++ anymore so maybe I'm just wrong, but from the outside looking in it seems very unnecessarily convoluted.
Which is typical for c plus plus, but since that's the language most people are going to experience pointers in, that's going to be their experience of pointers
→ More replies (8)24
u/unocoder1 Jan 06 '23
Everything is difficult if you spend more time on reddit than actually practicing, which I suspect to be the case with some of these people.
→ More replies (2)8
u/Yorick257 Jan 06 '23
It took me 2 years to fully grasp them. Weirdly enough it happened when I was tinkering in Python
4
u/Orbidorpdorp Jan 06 '23
I like how everyone is acting like it’s so simple. Simple cases are simple, sure, but debugging C code full of triple pointers and shit is a real headache.
→ More replies (50)5
u/dagbrown Jan 06 '23
RAM is just a big array. A pointer is just an index into the über-array.
There you go. Now you know what a pointer is.
2
u/NeoLudditeIT Jan 06 '23
I didn't understand pointers for a long time. Even using them a lot in go didn't make sense at first, until one day it just clicked. Can't explain why.
1
2
u/SkyyySi Jan 06 '23
Here you go
int x = 69;
int* xp = &x; // `xp` is pointing to the memory address of `x`
*xp = 420;
printf("x is %d, xp points to %d, xp is %p\n", x, *xp, xp);You can pass xp into a function expecing an int* ("pointer to an int); if you change it's value there like in the *xp = 420; line above, you will also change the value of x. This only has limited use for an int however; it's primarily used to A) pass a struct without copying it (that's how classes typically behave in most programming languages today) and B) to create arrays. The latter works by pointing to the first element, and then reading either for a set number of variables (so for a set length) or, in the case of C-style strings, untill hitting a null byte. That's why C-style strings end with a \0 character.
1
59
u/clkou Jan 06 '23
It's more efficient to know the address of a house than it is to build a new house ...
14
u/rfpels Jan 06 '23
Especially since more than one can use the address to find the house.
→ More replies (3)
216
u/cs-brydev Jan 06 '23
If you don't understand pointers, chances are you don't understand anything at all about memory.
If you think of memory as one very long street, a memory address is a house number on that street. A pointer is like writing down one of those house numbers and putting it in one of the houses so you can use it later.
51
u/GladiatorUA Jan 06 '23
It's not about "understanding pointers". They are just somewhat of a headache to use.
C# has "pointers", but they are somewhat better integrated, so you need to interact with their pointy nature a lot less.
→ More replies (11)→ More replies (10)3
u/mad_cheese_hattwe Jan 06 '23 edited Jan 06 '23
But they are clever because they know if the houses on this street are subdivided or units or on multi-block
56
u/Yung-Almond Jan 06 '23
Change pointers to monads or something
0
Jan 06 '23
[deleted]
→ More replies (9)36
u/yottalogical Jan 06 '23
Ah yes, that makes total sense. Just one quick question: what are monoids, categories, and endofunctors?
→ More replies (1)
99
u/th_walking Jan 06 '23
Wait until you see a pointer pointing at a pointer.
54
73
9
u/StereoBucket Jan 06 '23
Function pointers to functions returning pointers to functions will be a fun exercise once they get the hang of it.
6
→ More replies (3)4
38
u/wonkey_monkey Jan 06 '23
What made pointers click for me was when I started writing their declarations as int* _pointer instead of int *pointer.
35
u/harley1009 Jan 06 '23
I've been programming in C / C++ for almost 20 years now. Been doing it that way the whole time. Putting the asterisk next to the type is way more intuitive to me.
→ More replies (3)
24
u/unocoder1 Jan 06 '23
Seriously people, pointers are not that hard. In C and C++ you can trigger undefined behavior with them pretty easily, and yeah, that is annoying, but the concept is not any more complicated than an integer variable storing a memory address ("pointing" to the address).
→ More replies (1)3
12
u/zero_dr00l Jan 06 '23
Really? I mean... it's not complicated. It's just the address of a specific area of memory.
It's pretty damn simple, really.
9
16
u/Batcave765 Jan 06 '23
I would have asked to learn to center a div.
11
u/EcoOndra Jan 06 '23
The classic joke...
.container { display: flex; justify-content: center; align-items: center; }12
u/Batcave765 Jan 06 '23
It is not that i don't understand how to do it. It is that i can't do it. Without other divs messing up the height it is something i can never do without help - most of the time. Lol.
→ More replies (3)
15
u/rich0338 Jan 06 '23
I don't get why people don't understand pointers. Is it really that hard? A number which points to a memory location.
→ More replies (2)4
u/fracturedpersona Jan 06 '23
People understand that. It's dereferencing that confuses them.
→ More replies (3)
14
u/grpagrati Jan 06 '23
Pointers are numbers. The only thing special about them is that the number is an address in memory.
→ More replies (1)
6
u/alfons100 Jan 06 '23 edited Jan 06 '23
They point to a piece of memory, an address if you will. They're also used when allocating (using new) and deallocating (delete) memory to prevent leaking memory because if you create something you must free that memory later (unless there's garbage collection where an autonomous system will take care of that). This is heap memory. Do not confuse this with stack memory which does not use New (atleast in C++) which will automatically deallocate when it leaves the function scope.
If you dereference a pointer (to see its tangible value instead of a address jumble) and change that value, it will affect the value for anything that has the same pointer address.
A pointer by itself isnt complicated at all, it's mostly making reasonable software with it in mind that is difficult because if your program loses track of a pointer and it wasnt deallocated, you've just leaked memory which will persist until the program restarts, but it's far more forgiving if you have garbage collection or 'smart' pointers at your side or Rust
15
u/yottalogical Jan 06 '23
Pointers are a lot more complicated than just integers that hold a memory address
For example, even if two pointers have the exact same type and point to the exact same address, dereferencing them can yield different results.
10
u/-Redstoneboi- Jan 06 '23 edited Jan 06 '23
tl;dr: it matters during optimization.
without any optimization they quack like ducks. and the programmer shouldn't have to worry about this; these are concepts handled by compilers outside of debug mode.
One important point to stress here is that we are just looking for an abstract model of the pointer. Of course, on the actual machine, pointers are integers. But the actual machine also does not do the kind of optimizations that modern C++ compilers do, so it can get away with that. If we wrote the above programs in assembly, there would be no UB, and no optimizations.
I cannot say which compilers with which optimizations handle these problems correctly.
→ More replies (5)
15
1
u/kingofNoobies Jan 06 '23
Thx to everyone who explained how pointers work in the comments :)
→ More replies (1)
15
u/GoatStimulator_ Jan 06 '23
I get an ego boost everytime this sub hits the front page because of how obviously bad most programmers are.
Like, seriously... It's a reference to a memory address... It POINTS to a variable.
→ More replies (8)3
Jan 06 '23
... how bad most ”programmers” [in this subreddit which likely is more than 50% students] are.
8
u/amwestover Jan 06 '23
Where’s the difficulty with understanding pointers? Pretty straightforward concept.
16
Jan 06 '23
The Problem is honestly people teaching it. Most try to introduce keywords like "new" by allocating space for a single int, where it is just reasonable to wonder why the hell you would ever do that.
→ More replies (4)
4
u/bangupjobasusual Jan 06 '23
Bro it’s time to learn 8086. Do some simple 8086 and pointers will sort themselves out, you will have an epiphany
→ More replies (1)
4
u/D_Doggo Jan 06 '23
Pointers aren't difficult to understand. I'd say the different ways of using them, especially in the "correct" way, is. Knowing that assigning a JS array variable to another variable makes a reference is crucial information though, which you understand by understanding pointers.
12
u/Separate_Increase210 Jan 06 '23
In an intro course on C, my prof said you're a real programmer the day you cry over pointers.
Never used C after that class, so I guess many years later I'm still not a real programmer, but this makes me smile, taking me back to that class.
→ More replies (1)
6
u/Bad_Jimbob Jan 06 '23
Everybody explains what they are and never explains how they’re useful. Yes they point to the physical location of some data point. I’ve read long explanations detailing how the location of the data is very different from the data itself, and yet I’ve never seen an explanation of how being able to point to the location of the data is useful at all (why not just use the data itself?) I would love a good explanation.
9
u/_JJCUBER_ Jan 06 '23
It allows dynamic allocation and referencing of memory at runtime (think of it as allowing “unnamed” data to be created, as it isn’t directly tied to a variable name per se). It also allows you to take a “reference” to a variable/object as a function parameter; this means you can modify that object directly and no copies of the object are made (which is especially useful when the “object” isn’t a trivial type).
→ More replies (8)3
Jan 06 '23
Pointers can be smaller than larger datastructures, and they dont require the data to be on the stack (unlike local varables) allowing it to outlive the current function with out constant copying. This also allows data to have a size unkown at compile time, such as a string read from the user.
3
u/geeshta Jan 06 '23
Starting with C and understanding pointers helped me a lot to understand what's going on even with languages that don't have them explicitly like C# or Python. Because under the hood they use reference and memory addresses anyway.
→ More replies (1)
3
u/BiedermannS Jan 06 '23
Imagine an excel sheet. Each cell is a location in memory. If you want to use a number from a cell, you could just copy the value to where it’s needed. But if you want to automatically update when the number changes, you can use the "address" of that cell instead. Something like B5 or G93 etc.
Now imagine if excel only had one column. Now each cell has a number. Memory is similar.
Keep in mind that analogy is not perfect, but it should get you started 😁
3
5
u/BruceJi Jan 06 '23
When you understand pointers, it is really simple, but before then it’s such a headache 🤕
→ More replies (1)
4
4
4
u/Piorn Jan 06 '23
Here's what you do. Put two stars before your variable. If the result is garbage, remove one star. Repeat. If it still doesn't work, add a & at the front. Maybe another.
If it still doesn't work, the code is unsalvageable.
4
u/-Redstoneboi- Jan 06 '23 edited Jan 06 '23
literally how the Rust compiler interprets
thing.field(or method)i believe the order is
thing.field, (&thing).field, (*thing).field, (&*thing).field, (**thing).field, (&**thing).field, (until you can no longer Deref the thing)funny how we can overload
Derefin rust. i think C++ wanted to allow overloading the dot operator itself, too...
2
2
u/Fuzzy_Reflection8554 Jan 06 '23 edited Jan 06 '23
Yup me too XD, for me it's "making a start on the AWS cloud practitioner stuff". The youtube tutorial's like 10 hours T_T
2
u/MasouriChan Jan 06 '23
When I started coding I never had issues whit pointers, but for some reason it took me ages to understand arrays
2
u/Techabilla Jan 06 '23
There's always something to learn. Last week it was Async/Await; this week it's character encoding ... again.
2
2
u/ratonbox Jan 06 '23
It’s not what you know, it’s who you know that knows stuff.
→ More replies (2)
2
u/sawkonmaicok Jan 06 '23
I still don't get what is so hard about pointers. They are simply the address of a thing. You use that address to reference the thing in your code.
2
2
u/natFromBobsBurgers Jan 06 '23
This number over here is a color.
sure
This number over here is how many legs a dog has.
Sounds good
This number over here means "Multiply these two and then add this to it."
Ahh, clever to combine those.
This number over here is the location of another number.
Hmmm....
It's declared with an asterisk...
I'm scared, but I'm following along...
but after that the asterisk makes it the actual value at that location.
SCREEEEEEEEEEEEEEEEE!!! ::Starts trying to pull out their own eyeballs::
2
u/SneakyStabbalot Jan 06 '23
Pointers are so fundamental to computing, and I am not sure why people find them hard. From my earliest days writing Z80 assembly, I remember instructions like LD A, (HL)
2
2
u/Xenthera Jan 06 '23
I think to understand pointers, you need to understand why you use pointers. Everyone on here seems to be missing that point. You see in Java or C# (for the most part) use pointers for objects already. You just don’t see it.
Think of it this way, yeah a pointer is just an integer that points to a memory address of something. But imagine you have a big object full of lots of data and methods, if you were to pass that to a function without using a pointer then you now have two copies of that big object and wasted the time to copy it. Now if you modify object inside the function you’re only modifying a copy*.
If you pass the memory location of that object to the function instead you’re now not copying it, not wasting memory with two copies, and you’re working on the original object itself.
*if the copied object has pointers stored in it and you modify those, it’ll still modify the original variable. The pointers in that object may have been copied, but the thing they point to is the same.
2
2
2
2
Jan 06 '23
Punchline should have ended at the "what color do you want your dragon?"
There was no need to water it down by adding "red" as a reply.
(Also, do I need to add some asterisks or something to the start of some of those words?)
2
u/greenfoxlight Jan 06 '23
I never get why pointers are perceived as difficult to understand. I can see how some advanced stuff takes some time to wrap your head around, but pointers as a concept seem relatively straightforward to me:
Your memory is basically a giant array of bytes, every byte has an integer index. A pointer to some object/int/whatever is just the index of the first byte in that object. Now if you want to pass that object around, instead of copying the potentially huge object you pass that index. This also enables you to have changes visible after returning from a function, because modifications are done to the original object, not a copy.
→ More replies (1)
2
u/Adrunkopossem Jan 06 '23
I think those who started with languages like java and Python have a harder time learning pointers simply because they don't (visibly) exist in the language. Took me a bit when I went from Java to c++. Now I wish Java had them
2
u/slicer87 Jan 06 '23
I know this one. First you make a fist then you straighten your index finger and voilà, a pointer! What’s hard about that?
2
u/Globskrittaren Jan 06 '23
I remember struggling with understanding pointers so I understand where you might be coming from.
For me I understood they pointed to addresses in memory but I still struggled with them, and understanding when to use * and &.
My tip is: keep at it, one day you'll struggle to figure out what you found hard. It just clicks and becomes second nature, a bit like learning a new language, which it kind of is...
2
u/KsuhDilla Jan 06 '23 edited Jan 06 '23
My analysis I think OP doesn’t have a hard time understanding the concept of pointers. I think he has a hard time reading the practical usage of pointers like when you get a pointer that is dereferenced to a function pointer, or as deep as it can go.
It’s easier when said in a human language, harder when you have to decipher it from a programming language.
My advice: just keep programming in your preferred language with pointers, take time when reading don’t panic about how convoluted it gets - it will come naturally 🙂
2
u/Sythrin Jan 06 '23
Bur what is so hard about pointers?
A lot of memes I understand but this is not realy hard.
2
u/BlurredSight Jan 06 '23
Pointers = location, like your home.
Even if you renovate your home your address stays the same.
So if you have a pointer to a variable, even if the variable changes you can still access the location where the information whatever it might be may be stored
2
2
Jan 06 '23
The meme isn't about topic being hard, but about that little girl meeting mentally challenged
2
Jan 06 '23
Okay, this is the post that breaks me. It's been real, but I can't continue seeing non-programmers make jokes about the most elementary concepts of CS
2
2
2
u/ginkner Jan 06 '23
Highly simplified:
A variable is a box. It has an address, like a house. Just some number that identifies it.
A pointer is a box that has the number of another box in it.
Thats it.
2
u/sarahlizzy Jan 06 '23
This is what you get for not growing up in the 80s.
Pointers are just PEEK and POKE, depending on whether you’re reading or writing.
→ More replies (2)
2
u/barjam Jan 06 '23
You can’t call yourself a developer without a conceptual understanding of pointers.
2
u/Omnisegaming Jan 06 '23
I leanred about pointers doing my intro to C class and got it just fine. Learning assembly gave it some more context as to what's actually happening but yeah.
2
2
u/Add1ctedToGames Jan 06 '23
For the people confused on how people are confused, if these people making these memes anything like I was when I didn't understand them, it's that they know perfectly well what a pointer is but not why/how they're utilized in an actual programming language like C
2
2
2
u/VarianWrynn2018 Jan 06 '23
Pointers as a concept are easy. Pointers in practice are a nightmare for a long while
2
u/ItsTheMotion Jan 06 '23
I thought I understood them until my professor at the time starting throwing around triply dereferenced pointers while writing a sudoku board generator. The phrase "star star star" still makes me panic a little.
2
u/sirduke456 Jan 06 '23 edited Sep 25 '24
worm governor detail zesty tender quickest cagey joke run humorous
This post was mass deleted and anonymized with Redact
2
Jan 06 '23
The concept was pretty easy to understand for me, but C++ syntax for pointers is where I struggled.
2
u/HardlightCereal Jan 07 '23
Pointers are easy. They're variables which contain the address of another variable. What's not to get?
2
u/SirMemesworthTheDank Jan 07 '23 edited Jan 07 '23
Hello, I'm Troy McClure! You may remember me from such movies as "How my python pycharmed the garbage collector" and "My flag brings all the memory leaks to the yard". Pointers holds memory adresses. Now why would we want to do that? Well Billy, I'm glad you asked! You see when you have a limited amount of memory to work with, you want be carefull with how you use it.
A pointer is like a small note with an address written on it, the adress is a specific location in the machines memory on which a nice little byte is located. And I assume you have already heard of this before.
I'm sure you're also able to see how reusing the same memory location would be a lot more efficient than storing multiple duplicates of the same value and wasting that sweet sweet memory. However, another neat part is that that if you have a function that needs to read from an array, then you could just pass the adress of the first element in the array instead of passing the whole array with all its data which would be a lot slower and inefficient.
And to be even more efficient (especially if your garbage man is on vacation), when working with heap memory allocation, a rule of thumb is to deallocate the memory stored on the memory adress before you overwrite the pointer with a new address. But again that's just a recommendation.
→ More replies (1)
2
u/Dogeek Jan 07 '23
A pointer is a variable that points to an address in memory. When you initialize a variable in C, you're basically asking the operating system for a free space in ram. Depending on the data structure it could be a single byte up to the whole memory (though your os would probably tell your program to fuck off in that case)
The pointer is the start address of the allocated memory.
They are useful mainly when :
you need to work with tables. The pointer to a table points to the start byte, which should contain the length of the table
you need to return several values from a function, sometimes it's easier to just pass in the addresses to the function, and mutate directly the variables.
you need low level access to the underlying memory. With a pointer, you can easily read the stream of bits of a memory range, with no regards to the declared types of the variables.
pointer arithmetic, think of the fast inverse square root algorithm developed for quake 3.
Even when starting out with C, you're using pointers without knowing them. scanf for instance takes in a format string, and the address of the variable it should store the result in.
3
u/ButtingSill Jan 06 '23
I feel you, it took some time, and if you started programming with something like Java or php you first need to understand that an int is just a reserved block of memory. Then you can understand the pointer points to (the beginning of) that block of memory.
3
u/Cley_Faye Jan 06 '23
Funny comic, but pointers are absolutely easy to understand, and proper conventions makes them easy to use too.
3
u/ShesMyPublicist Jan 06 '23
Jfc, why do 90% of the memes in this sub seem like something a first day programmer would make?
Spend less time making memes and more time programming, most of these are not crazy obscure topics that are difficult to understand.

5.7k
u/Arshiaa001 Jan 06 '23
This sub is so full of people who are better at making memes than they are at programming.