of this tutorial is to provide an introduction to pointers and their use to these beginners.
I have found that often the main reason beginners have a problem with pointers is that
they have a weak or minimal feeling for variables, (as they are used in C). Thus we start
with a discussion of C variables in general.
A variable in a program is something with a name, the value of which can vary. The way
the compiler and linker handles this is that it assigns a specific block of memory within
the computer to hold the value of that variable. The size of that block depends on the
range over which the variable is allowed to vary. For example, on PC's the size of an
integer variable is 2 bytes, and that of a long integer is 4 bytes. In C the size of a variable
type such as an integer need not be the same on all types of machines.
When we declare a variable we inform the compiler of two things, the name of the
variable and the type of the variable. For example, we declare a variable of type integer
with the name k by writing:
int k;
On seeing the "int" part of this statement the compiler sets aside 2 bytes of memory (on a
PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the
symbol k and the relative address in memory where those 2 bytes were set aside.
Thus, later if we write:
k = 2;
we expect that, at run time when this statement is executed, the value 2 will be placed in
that memory location reserved for the storage of the value of k. In C we refer to a
variable such as the integer k as an "object".
In a sense there are two "values" associated with the object k. One is the value of the
integer stored there (2 in the above example) and the other the "value" of the memory
location, i.e., the address of k. Some texts refer to these two values with the nomenclature
rvalue (right value, pronounced "are value") and lvalue (left value, pronounced "el
value") respectively.
In some languages, the lvalue is the value permitted on the left side of the assignment
operator '=' (i.e. the address where the result of evaluation of the right side ends up). The
rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues
cannot be used on the left side of the assignment statement. Thus: 2 = k; is illegal.
Actually, the above definition of "lvalue" is somewhat modified for C. According toK&R II (page 197): [1]
"An object is a named region of storage; an lvalue is an expression
referring to an object."