Você está na página 1de 8

Problem Solving and Program Design in C

(5th Edition)

by Jeri R. Hanly and Elliot B. Koffman

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

 Notice the difference between the 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"
Pointer
Declaring Variables of Pointer Types
 Due to the ability of a pointer to directly refer to the value
that it points to, it becomes necessary to specify in its
declaration which data type a pointer is going point to.
 Declarations:
int *number;
char *character;
float *greatnumber;
Pointer
Example 1
// my first pointer
#include <iostream>
using namespace std;
int main ()
{
int firstvalue, secondvalue;
int *mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
printf("firstvalue is &d\n", firstvalue);
printf("secondvalue is &d\n", secondvalue);
return 0; firstvalue is 10
} secondvalue is 20
Pointer (Example 2)
Function with Output Arguments
void /*-this function returns 2 results-*/
calculation(int num, /* input */
int *cal1, /* output */
int *cal2) /* output */
{
*cal1 = num + num;
*cal2 = num * num;
}

int main(void)
{
int value; /* input: number entered by user */
int sum; /* output: num + num */
int multi; /* output: num * num */

printf(“Enter a value to analyze> “);


scanf(“%lf”, &value);
calculation(value, &sum, &multi);
printf(“Sum = %d, Multiply = %d”, sum, multi);

return(0);
Pointer
(Example 3)

Você também pode gostar