Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of "referencing" and "dereferencing" in C

I read different things on the Internet and got confused, because every website says different things.

I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing is accessing the value of the variable that the pointer points to. So I got confused.

Can I get a simple but thorough explanation about "referencing and dereferencing"?

like image 243
Milkncookiez Avatar asked Jan 08 '13 22:01

Milkncookiez


People also ask

What is the use of referencing and dereferencing operators?

A reference variable provides a new name to an existing variable. It is dereferenced implicitly and does not need the dereferencing operator * to retrieve the value referenced. On the other hand, a pointer variable stores an address. You can change the address value stored in a pointer.

What is referencing operator in C?

Reference operator (&) In C Language. This referencing operator is also called address operator, which gives the address of a variable, in which location the variable is resided in the memory.

Why is it called dereferencing?

Dereferencing means taking away the reference and giving you what it was actually referring to. A pointer to something really means that your pointer variable holds a memory address of something . But the pointer can also be thought of as a reference to something instead.


1 Answers

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 retrieve the value from the memory address that is pointed by the 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:

  • Dereferencing an 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 

http://en.wikipedia.org/wiki/Dereference_operator

The dereference operator * is also called the indirection operator. 
like image 182
A B Avatar answered Oct 16 '22 08:10

A B