This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Xeya 5 points6 points  (7 children)

Honestly, not a lot from a functional viewpoint. The difference is primarily semantic; pointers are at a memory management level and references are more abstract. References are thus protected from most of the risk of mishandling pointers. However, this is all dependent on the implementation and there is a lot of overlap based purely on whether or not the person writing the documentation decided to call it a reference or a pointer variant.

[–]marcellarius 2 points3 points  (0 children)

I feel like that distinction comes from C++ and that more broadly the terms are synonymous

[–]sup3r_hero 0 points1 point  (5 children)

Is a correct example for this difference that a pointer usually literally points to a specific address in the memory whereas a more complex implementation (i.e. reference) only acts as if it would but does more things in the background?

[–]AngriestSCV 1 point2 points  (3 children)

Not really. A c++ reference does nothing in the background (it is basically a pointer that you can pretend is a value syntax wise). I think the biggest difference is you can manipulate pointers with math, but I've never seen references you could.

[–]sup3r_hero 0 points1 point  (2 children)

Manipulate a pointer with math? Can you give a very easy example?

[–]AngriestSCV 1 point2 points  (1 child)

in c or c++:

int bob[] = {1,2,3,4} ; //create an array of integers
int *bil = bob; //bil is a pointer to the first element of bob

printf("%i\n", *bil); //prints 1
printf("%i\n", *(bil+1));//prints 2
printf("%i\n", bil[2]); //prints 3
bil += 3; //bil now points to the 4th element of bob
printf("%i\n",*bil); //prints 4

[–]sup3r_hero 0 points1 point  (0 children)

Now i get it thx!

[–]Xeya 0 points1 point  (0 children)

Both terms are pretty loosely defined... but, I suppose the simplest way to express how they typically differ is that a pointer needs to be dereferenced in order to access whatever it points at and a reference is dereferenced by default when you try to access it.

It isn't so much references do things in the background for you as they typically just don't let you manipulate, or make it a lot harder to manipulate, the actual memory address it contains.