You are viewing a single thread.
View all comments
36 points

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.

permalink
report
reply
15 points

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

permalink
report
parent
reply
6 points
*

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

permalink
report
parent
reply
4 points

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.

permalink
report
parent
reply
4 points

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

permalink
report
parent
reply
2 points

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.

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
1 point

pointers are just symlinks in programming

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 4.3K

    Monthly active users

  • 945

    Posts

  • 10K

    Comments

Community moderators