Download presentation
Presentation is loading. Please wait.
Published byBernice Murphy Modified over 9 years ago
1
C Programming Lecture 16 Pointers
2
Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory. b But the value that is stored in a pointer is always the address of another memory location.
3
The Use of Pointers in C b Pointers are used in function calls. b Pointers are used with arrays and strings. b Pointers are used to access files. b Pointers are used with structures.
4
Syntax for Pointer Declarations and Assignments b Pointer variables can be declared in programs and then used to take addresses as values: int i, *p;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;p = &i; assigns the address in memory named by i to the pointer variable p.
5
Some Groundwork for an Example b 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.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.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.
6
Example Declaration and Assignment int i = 25, j = 13, *p; int i = 25, j = 13, *p; three memory locations have been chosen by the system (assume they are the following): three memory locations have been chosen by the system (assume they are the following): 00011001 98 is the address named i... 00001101 204 is the address named p 00000000... 116 is the address named j 2513 nothing assigned
7
Now Some New Assignments i = j; p = & i; /* & is the address operator */ 00001101 98 is the address named i... 00001101 204 is the address named p 01100010... 116 is the address named j 13 98 p now “points” to i (the value stored in p is the address of i ). i = j; p = & i;
8
Using p to Change What is in i 00100011 98 is the address named i... 00001101 204 is the address named p 01100010... 116 is the address named j 3513 98 *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. */
9
Understanding Pointers b The previous examples have used “made up” memory addresses and values to explain pointer declaration and pointer dereferencing.
10
Evaluating Pointer Expressions Declarations and initializations int i = 3, j = 5, *p = &i, *q = &j, *r; double x; Expression Equivalent expression Value p == &i p == (&i) 1 p = i + 7 p = (1 + 7) illegal * *&p *(*(&p)) 3 r = &x r = &x illegal 7 * * p / *q + 7 (((7 * (*p))) / (*q)) + 7 11 * (r = &j) *= *p (*(r = (&j))) *= (*p) 15
11
Pointers to void b In ANSI C, one pointer can be assigned to another only when: They both have the same type, orThey both have the same type, or When one of them is of type pointer to void (void *).When one of them is of type pointer to void (void *). b We can think of void * as a generic pointer type.
12
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;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.