c programming lecture 16 pointers. pointers b a pointer is simply a variable that, like other...

12
C Programming C Programming Lecture 16 Lecture 16 Pointers Pointers

Upload: bernice-murphy

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

C ProgrammingC Programming

Lecture 16Lecture 16PointersPointers

Page 2: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

PointersPointers

A A pointerpointer is simply a is simply a variable that, like other variable that, like other variables, provides a name variables, provides a name for a location (address) in for a location (address) in memory.memory.

But the But the valuevalue that is stored that is stored in a pointer is always the in a pointer is always the address of another memory address of another memory locationlocation..

Page 3: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

The Use of Pointers in CThe Use of Pointers in C

Pointers are used in Pointers are used in function function callscalls..

Pointers are used with Pointers are used with arrays arrays and stringsand strings..

Pointers are used to access Pointers are used to access filesfiles..

Pointers are used with Pointers are used with structuresstructures..

Page 4: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Syntax for Pointer Syntax for Pointer Declarations and AssignmentsDeclarations and Assignments

Pointer variables can be declared Pointer variables can be declared in programs and then used to take in programs and then used to take addresses as values:addresses as values:• int i, *p;int i, *p;

declares declares ii to be a variable of type to be a variable of type int and int and pp to be a variable of type to be a variable of type “pointer to int”.“pointer to int”.

• p = &i;p = &i;assigns the address in memory named assigns the address in memory named by by ii to the pointer variable to the pointer variable pp..

Page 5: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Some Groundwork for an Some Groundwork for an ExampleExample

Let’s state some assumptions that we Let’s state some assumptions that we will use in an example.will use in an example.• Every byte in our computer’s RAM (random Every byte in our computer’s RAM (random access memory) is numbered sequentially access memory) is numbered sequentially starting with zero.starting with zero.– We refer to this number as the We refer to this number as the addressaddress of a of a particular byte.particular byte.

• When we declare a variable we give it a type When we declare a variable we give it a type and a name.and a name.– We can think of the We can think of the namename as a name for the as a name for the beginning addressbeginning address of the bytes that the system of the bytes that the system assigns to the declared variable -- how many assigns to the declared variable -- how many total bytes are assigned (they will be total bytes are assigned (they will be sequential bytes) depends on the sequential bytes) depends on the typetype of the of the variable.variable.

Page 6: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Example Declaration Example Declaration and Assignmentand Assignment

int i = int i = 2525, j = , j = 1313, , **p; p;

three memory locations have been chosen by the system (assume they are the three memory locations have been chosen by the system (assume they are the following):following):

00011001

98 is theaddress named i

. . . 00001101

204 is the addressnamed p

00000000. . .

116 is theaddressnamed j

25 13nothingassigned

Page 7: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Now Some New Now Some New AssignmentsAssignments

i = j;p = & i; /* & is the address operator */

00001101

98 is theaddress named i

. . . 00001101

204 is the addressnamed p

01100010. . .

116 is theaddressnamed j

13 13 98

p now “points”to i (the value stored in p is theaddress of i ).

i = j;

p = & i;

Page 8: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Using p to Change What is Using p to Change What is in iin i

00100011

98 is theaddress named i

. . . 00001101

204 is the addressnamed p

01100010. . .

116 is theaddressnamed j

35 13 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. */

Page 9: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Understanding PointersUnderstanding Pointers

The previous examples have The previous examples have used “made up” memory used “made up” memory addresses and values to addresses and values to explain explain pointer declarationpointer declaration and and pointer dereferencingpointer dereferencing..

Page 10: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Evaluating Pointer Evaluating Pointer ExpressionsExpressions

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

Page 11: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Pointers to voidPointers to void

In ANSI C, one pointer can be In ANSI C, one pointer can be assigned to another only when:assigned to another only when:• They both have the They both have the same typesame type, or, or• When one of them is of When one of them is of type pointer type pointer to void (to void (void *void *))..

We can think of We can think of void *void * as a as a generic pointer type.generic pointer type.

Page 12: C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory

Illegal and LegalIllegal and LegalPointer AssignmentsPointer 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;