this post was submitted on 12 Dec 2024
255 points (99.2% liked)

Programmer Humor

32716 readers
664 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

you are viewing a single comment's thread
view the rest of the comments
[–] SelfProgrammed@lemmy.today 40 points 1 week ago (6 children)

Here's a secret I wish someone told me in high school: literally everything is pass by value, it's just that sometimes the value is an address. It demystified pointers for me.

[–] Swedneck@discuss.tchncs.de 1 points 5 days ago

pointers are just symlinks in programming

[–] MyNameIsRichard@lemmy.ml 17 points 1 week ago

Yeah, when I grokked that simple fact pointers became easy.

[–] ddplf@szmer.info 8 points 1 week ago* (last edited 1 week ago)

It's the opposite, everything is passed by reference but primitives are also addresses and therefore passed by value

You can't pass objects or functions as value

[–] asudox@discuss.tchncs.de 7 points 1 week ago (1 children)

That got me confused with rust references and why the dereference operator even exists as well.

[–] Ephera@lemmy.ml 3 points 1 week ago (1 children)

I mean, Rust has the additional thing that a reference is a pointer + a borrow, so it's not quite as similar to a pass-by-value.

And as for the dereference operator, occasionally you can use it to turn a reference into an owned value, often by making a copy of the value (but in that case, you can usually also use .to_owned()).

A case where I don't think there's an alternative to the dereference operator, is if you've got a mutable reference and you want to replace the value that's underneath: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=04621490e1d0fd6fe1af7f2e843547fb
At the very least, if you remove the asterisk, the compiler will tell you very precisely that you need to add it back.

[–] asudox@discuss.tchncs.de 1 points 1 week ago

yeah. references aren't the same as pointers in c++ but similar, so it's something along those lines.

[–] underisk@lemmy.ml 5 points 1 week ago

They taught you about pointers in high school? The only course available to me that even touched on programming just covered how to use C to do conditionals, read keyboard input, and print text to a terminal. The bulk of the course was learning MS Office.

[–] propter_hog@hexbear.net 3 points 1 week ago

That's true in Java, not necessarily in other languages