+44(0) 1234 567 890 info@domainname.com

August 20, 2010

What is Pointer ?

2:12 PM

Share it Please
Pointers are a very powerful feature of the C++ language that has many uses in advanced programming. Quote from C++ Tutorial whos published by Juan Soulie, pointer is a variable which stores a reference to another variable. Translated to Bahasa Indonesia "Pointer adalah variabel yang menyimpan referensi ke variabel lain."

Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".


Pointer may call as dereference (*). It is complement of reference (&), the address that locates a variable within memory. As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address). We generally do not actively decide the exact location of the variable within the panel of cells that we have imagined the memory to be - Fortunately, that is a task automatically performed by the operating system during runtime. However, in some cases we may be interested in knowing the address where our variable is being stored during runtime in order to operate with relative positions to it.

As ilustration:

kirman == 25
&kirman == 1776
ted == 1776
*ted == 25

The first expression is quite clear considering that the assignment operation performed on kirman was kirman=25. The second one uses the reference operator (&), which returns the address of variable kirman, which we assumed it to have a value of 1776. The third one is somewhat obvious since the second expression was true and the assignment operation performed on ted was ted=&kirman. The fourth expression uses the dereference operator (*) that, as we have just seen, can be read as "value pointed by", and the value pointed by ted is indeed 25.

0 komentar: