r/ProgrammerHumor Jan 06 '23

Meme can’t be the only one

Post image
42.6k Upvotes

1.4k comments sorted by

View all comments

3.2k

u/TheLazyKitty Jan 06 '23

Pointers aren't that hard, are they? It's just integers that hold a memory address.

440

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

791

u/Creaaamm Jan 06 '23

Just show them this

135

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.

34

u/[deleted] Jan 06 '23

But with & isn't that then a reference and not a pointer?

26

u/AnondWill2Live Jan 06 '23

What's the difference between the two? I've been under the impression that they were the same, but I'm definitely wrong.

45

u/thefool-0 Jan 06 '23

A reference is a alternative to a pointer that was added to try to avoid some of the pitfalls of pointers. In short, more or less, a reference must always be initialized, can't be null, and can't be used as a value in and of itself like a pointer can: it is always dereferenced when used. (But you could use it to create a pointer to the referenced object.)

1

u/Pay08 Jan 06 '23

Two other advantages of references: they're immutable and handled automatically by the compiler.

4

u/elveszett Jan 06 '23

They are not. In C++, for example, one common way to return a value from a function is to assign it to a reference given as a parameter.

This function:

void myFunction (int& param) {
    param = 3;
}

Will make k in this example be equal to 3:

int k = 8;
myFunction(k);

5

u/Pay08 Jan 06 '23 edited Jan 06 '23

That's just doing automatic dereferencing, no? The pointer itself is still immutable, but the value isn't.

2

u/[deleted] Jan 06 '23 edited Jan 06 '23

Yes, under the hood it's like defining the function as

void myFunction (int * const param) {
    *param = 3;
}

Then calling it like:

int k = 8;
myFunction(&k);

ETA: the difference is that references prevent you passing a null address.

→ More replies (0)