Videos
Here is a memory map; a representation of memory as a sequence of blocks:
address 01 02 03
+----+----+----+...
data within | 23 | 6f | 4a |
+----+----+----+...
Now suppose we create a character:
char c = 'z'; // 'z' is 7a in hex
Further suppose c is stored at address 01, so our memory looks like so:
address 01 02 03
+----+----+----+...
data within | 7a | 6f | 4a |
+----+----+----+...
Now, let's create a pointer:
char* p = &c; // point at c
p may be stored at address 02:
address 01 02 03
+----+----+----+...
data within | 7a | 01 | 4a |
+----+----+----+...
Here the pointer p is at address 02 and it points at address 01. That's the meaning of p = &c;. When we dereference the pointer p (at address 02) we look at what's in the address pointed at by p. That is, p points at address 01, and so dereferencing p means looking inside address 01.
Finally, lets create a reference:
char& r = c;
Here the memory layout doesn't change. That is, no memory is used to store r. r is a sort of alias for c, so when we refer to r we practically refer to c. r and c are conceptually one. Changing r means changing c, and changing c means changing r.
When you create a reference you must initialize, and once initialized you cannot re-initialize it with another target. That is, above the reference r means and forever will mean c.
Also related are const references. These are the same as a reference, except they are immutable:
const char& r = c;
r = 'y'; // error; you may not change c through r
c = 'y' // ok. and now r == 'y' as well
We use const references when we are interested in reading the data but frown upon changing it. By using a const reference the compiler will not copy the data, so this gives us ideal performance, but also forbid us from changing the data, for correctness.
In a sense, you can say that references are a compile-time feature, whereas pointers are a runtime feature. So references are faster and cheaper than pointers, but come with certain constraints and implications. Like other compile-time-vs-runtime alternatives, we sometimes pick one over the other for performance, sometimes for static analysis and sometimes for flexibility.
Time to go on a term-bashing spree, because these things always cause confusion.
A pointer is a memory address in its own right. Cue fancy diagram for how it happens in memory:
| Address | Value | |----------|----------------| |0x1111 |0x1112 | <-- Pointer! |0x1112 |42 | <-- Pointed value |0x1113 |42 | <-- Some other valueI've used a much smaller address size just for simplicity. Basically,
0x1111is a pointer because its contents are the address of another value.Dereferencing means examining the value of the address held in the pointer's value. Such fancy language can be confusing; basically, if I dereference
0x1111I look at0x1112and get the value out of that address. Why? Because it's really useful and because assembly lets us do it too,mov rax, [r8]Is nasm/intel syntax for "look in r8, find that memory address, follow it and find the value at that memory address and put that in rax".
Pass by value. Pass by value means that when you create a function stack frame, which is the stack contents around a function, you copy every value that is an argument to wherever it goes. Registers, stack, wherever. Of course, if you copy a pointer's value, you're copying a memory address and thus creating another pointer pointing to the same memory. This is how functions like this:
void add(int* x) { *x = *x + 7; }Work.
Pass by reference. What that function above does is essentially pass by reference semantics as you will see them in say C++. The crucial and possibly only difference as the implementation is likely identical at the assembly level is that a reference is something the C++ compiler understands. Since the compiler is the language this is important. C understands pointers and manipulating memory, and so do C compilers, but they'll let you do whatever you like. You can't re-assign a reference, for example,
void cppadd(int& x) { int a = 7; x = &a; // doesn't work. }
So, to sum it up, references are on one level a language feature where the compiler understands where the source memory is and prevents modification of that source memory address. It understands you want to play with the value. Pointers are just that, memory addresses holding other memory addresses.
Wikipedia summarises it pretty well:
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations.
Yes, I have mentioned C++ when this question is only C, but I feel it is prudent to clarify how a term has become somewhat confused with the addition of later languages.
Try this
#include <iostream>
void change_val_by_ref(int &x)
{
x=100
}
void change_val_by_val(int x)
{
x=50;
}
int main()
{
int whatever=0;
std::cout<<"Original value: "<<whatever<<"\n";
change_val_by_ref(whatever);
std::cout<<"After change by ref: "<<whatever<<"\n";
change_val_by_val(whatever);
std::cout<<"After change by val: "<<whatever<<"\n";
}
The output you will see is:
0
100
100
Let's see what happened
change_val_by_refchanged the originalwhatever, because the ref was "pointing" to the VARIABLE.change_val_by_valdidn't change thewhatever, because the argument of the functionxhas just copied the value ofwhatever, and anything that happens toxwill not affectwhatever, because they are not related.
That's the point of passing by ref.
Imagine you have banana and orange placed in two different plates. You place them into big box with a robotic hand. You, obviously, can change their destination using you hand. But you want the robotic hand to do this task. You need to tell where the banana and orange are placed, so robotic hand can do its job. !Important robotic hand doesn't care about fruit, which is placed into these plates, it only cares about changing their positions. By using a pointer, you just tell a function the placement of an object you want to change. And you can set your local, suitable name for this. So pointer is just the name of a plate, which contains a certain value.