Escolar Documentos
Profissional Documentos
Cultura Documentos
(5th Edition)
CP 202
Chapter 6
Pointer
C uses pointers in three different ways:
C uses pointers to create dynamic data structures -- data
structures built up from blocks of memory allocated from the
heap at run-time.
C uses pointers to handle variable parameters passed to
functions.
Pointers in C provide an alternative way to access information
stored in arrays. Pointer techniques are especially valuable
when you work with strings. There is an intimate link between
arrays and pointers in C.
Pointer
Reference Operator (&)
The memory of your computer can be
imagined as a succession of memory andy = 25;
fred = andy;
cells.
ted = &andy;
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).
The address that locates a variable
within memory is what we call a
reference to that variable.
reference to a variable can be
obtained by preceding the identifier of 1. we have assigned the value 25
a variable with an ampersand sign (&), to andy (a variable whose
known as reference operator. address in memory is 1776).
2. copied to fred the content of
The variable that stores the reference variable andy.
to another variable is what we call a 3. copies to ted the reference
of andy.
pointer.
Pointer
Dereference Operator (*)
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".
andy = 25;
ted = &andy;
beth = *ted;
*(&andy) == andy
int main(void)
{
int value; /* input: number entered by user */
int sum; /* output: num + num */
int multi; /* output: num * num */
return(0);
Pointer
(Example 3)