what is the difference between pointer , reference and dereference in c?
C has pointers and you can pretty much do anything you want with those pointers, you can deference them, and you change the value of a pointer. In fact pointer arithmetic is quite common technique in C programming. In my younger days as a C programmer references was not a commonly used term when talking with other C developers.
References as a term is very commonly used with Java, C# and Object Oriented Languages. In the context of Java and Object Oriented languages a reference is a pointer to an object instance in memory. With a reference you can't do pointer arithmetic, and that is the key difference between pointers and references in my view.
Pointers allow for pointer arithmetic and dereferencing, references only allow for dereferencing and changing what the reference points to.
Pointer is an address of some data, e.g.
int* a
.
Here a
is really just the address where an int value is stored.
A reference, in contrast, is another name for some variable, an alias, e.g.
int a;
int &b = a
Here b
is just another name for a
: b++
has the same effect as a++
.
According to my experience, let me answer.
In C++, there are variables (normal variables, pointer variables, etc.) and references.
The compiler will assign an address to each variable. Obviously, the address of this variable cannot appear the same. It can be said that each variable is actually an address. What is the reference &, the reference is not a variable but a tag, so the compiler will not assign him an address, but it does not mean that it has no address, its address is the address of the variable or object it refers to, so it is used as a tag Used to set its address to the address of the variable or object it refers to when the compiler parses it. This creates a situation where the address is the same as the address of the variable or object it references, unbelief Can compile and ponder or try GDB!
Since the reference and the address of the object it refers to are the same, why should C++ introduce the concept of reference? The pointer can also achieve the goal, it is not an extra move. I say it's the main reason I think!
For consistency and consistency! for example:
class box {
private:
int l;
public:
box(int length = 0) : l(length){};
box operator+(const box& that) {
box b;
b.l = this->l + that.l;
return b;
}
box operator+(const box* that) {
box b;
b.l = this->l + that->l;
return b;
}
};
int main() {
box b1(2);
box b2(4);
box b3 = b1 + b2;
box b4 = b1 + &b2;
return 0;
}
Above I overloaded the + operator of the box object, using references and pointers as arguments. In the main two expressions are not written in the same way, both can achieve the same function, obviously using the way of reference can be convenient to express, the use of pointers is more complicated, in the case of a large amount of code, you may be this Things get dizzy. If you think there are more important reasons, please let me know by comment!
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.
as in "C++...":
In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run-time.
There's no explicit reference type in C like in C++. Anywhere anybody says "reference" in context of C language you can assume a pointer.