Lecture16.ppt

advertisement
C Programming
Lecture 16
Pointers
Pointers
A pointer is simply a
variable that, like other
variables, provides a name
for a location (address) in
memory.
 But the value that is stored
in a pointer is always the
address of another memory
location.

The Use of Pointers in C
Pointers are
calls.
 Pointers are
and strings.
 Pointers are
files.
 Pointers are
structures.

used in function
used with arrays
used to access
used with
Syntax for Pointer
Declarations and Assignments

Pointer variables can be declared
in programs and then used to take
addresses as values:
• int i, *p;
declares i to be a variable of type
int and p to be a variable of type
“pointer to int”.
• p = &i;
assigns the address in memory named
by i to the pointer variable p.
Some Groundwork for an Example

Let’s state some assumptions that we
will use in an example.
• Every byte in our computer’s RAM (random
access memory) is numbered sequentially
starting with zero.
– We refer to this number as the address of a
particular byte.
• When we declare a variable we give it a type
and a name.
– We can think of the name as a name for the
beginning address of the bytes that the system
assigns to the declared variable -- how many
total bytes are assigned (they will be
sequential bytes) depends on the type of the
variable.
Example Declaration
and Assignment
int i = 25, j = 13, *p;
three memory locations have been
chosen by the system (assume they are
the following):
00011001
98 is the
address
named i
. . .
25
00001101
116 is the
address
named j
. . .
13
00000000
nothing
assigned
204 is the
address
named p
Now Some New Assignments
i = j;
p = & i; /* & is the address operator */
p now “points”
to i (the value
stored in p is the
address of i ).
p = & i;
i = j;
00001101
98 is the
address
named i
. . .
13
00001101
116 is the
address
named j
. . .
13
01100010
204 is the
address
named p
98
Using p to Change What is in i
*p = 35; /* Used this way, * is called the dereference */
/* (or indirection)operator. The meaning here */
/* is to assign 35 to the memory location
*/
/* stored in p.
*/
00100011
98 is the
address
named i
. . .
35
00001101
116 is the
address
named j
. . .
13
01100010
204 is the
address
named p
98
Understanding Pointers

The previous examples have
used “made up” memory
addresses and values to
explain pointer declaration
and pointer dereferencing.
Evaluating Pointer Expressions
Declarations and initializations
int
double
i = 3, j = 5, *p = &i, *q = &j, *r;
x;
Expression
Equivalent expression
p == &i
p == (&i)
p = i + 7
p = (1 + 7)
* *&p
*(*(&p))
r = &x
r = &x
Value
1
illegal
3
illegal
7 * * p / *q + 7 (((7 * (*p))) / (*q)) + 7
11
* (r = &j) *= *p (*(r = (&j))) *= (*p)
15
Pointers to void

In ANSI C, one pointer can be
assigned to another only when:
• They both have the same type, or
• When one of them is of type pointer
to void (void *).

We can think of void * as a
generic pointer type.
Illegal and Legal
Pointer Assignments
Declarations
int
*p;
float *q;
void *v;
Legal Assignments
Illegal Assignments
p = 0;
p = 1;
p = (int *) 1;
v = 1;
p = v = q;
p = q;
p = (int *) q;
Download