what is the difference between pointer , reference and dereference in c?
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 value
I've used a much smaller address size just for simplicity. Basically, 0x1111
is 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 0x1111
I look at 0x1112
and 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.
Referencing means taking the address of an existing variable (using &) to set a pointer variable. In order to be valid, a pointer has to be set to the address of a variable of the same type as the pointer, without the asterisk:
int c1;
int* p1;
c1 = 5;
p1 = &c1;
//p1 references c1
Dereferencing a pointer means using the * operator (asterisk character) to access the value stored at a pointer: NOTE: The value stored at the address of the pointer must be a value OF THE SAME TYPE as the type of variable the pointer "points" to, but there is no guarantee this is the case unless the pointer was set correctly. The type of variable the pointer points to is the type less the outermost asterisk.
int n1;
n1 = (*p1);
Invalid dereferencing may or may not cause crashes:
Any dereferencing of any uninitialized pointer can cause a crash Dereferencing with an invalid type cast will have the potential to cause a crash. Dereferencing a pointer to a variable that was dynamically allocated and was subsequently de-allocated can cause a crash Dereferencing a pointer to a variable that has since gone out of scope can also cause a crash. Invalid referencing is more likely to cause compiler errors than crashes, but it's not a good idea to rely on the compiler for this.
References:
http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators
& is the reference operator and can be read as “address of”.
* is the dereference operator and can be read as “value pointed by”.
http://www.cplusplus.com/doc/tutorial/pointers/
& is the reference operator
* is the dereference operator
you can read wiki as well The dereference operator * is also called the indirection operator.
This text is taken from this link they have provided the same answer to the same question: meaning of "referencing" and "dereferencing"
A pointer's value is a memory address.
int a;
int* b = &a;
// b holds the memory address of a, not the value of a.
A reference is a pointer with a value (memory address) that refers to a desired item.
int a;
int* b = &a;
// b is a reference to a.
A dereference is a technique of grabbing the memory contents that a pointer references.
int a;
int* b = &a;
int c = *b;
// c dereferences b, meaning that c will be set with the value stored in the address that b contains.
Note that a C++ reference is a different thing than a C reference. A C++ reference is an abstract idea where C++ decides to allow you to use non-pointer syntax for most calls, but will automatically "do the right thing" in passing a pointer when needed.