W18-Pointers

W18-Pointers - Pointers 1 Pointers Yet another use for and...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
11/7/05 1 Pointers 11/7/05 2 Pointers • Yet another use for & and * •in t year = 5; year represents the value stored in a certain location (address) in memory. • The memory address , i.e. the address of year , is called a pointer to year -- it gives the location of year , not it's value. 11/7/05 3 The 'address of' operator, & •G iv en int years; then &years is the memory address of this variable. &years is a pointer constant. scanf("%d", &years) – scanf() needs the address of where to store the data.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
11/7/05 4 The de-reference operator * • An address of a variable can be called a reference to it. • Like a card-catalog entry for a book -- It refers to the book, but is not the book. • The * unary operator is the inverse of &. • It gets the thing referred to. It 'de-references' the reference. *&a is just the value of a 11/7/05 5 Pointer variables • A variable that can hold an address is called a pointer variable. • An integer pointer variable is declared as: int * p; int * is a new type called pointer to int • p is a variable of type pointer to int . • Only addresses of integers can be stored in p. 11/7/05 6 More Syntax int *p, years, a; years = 20; p = &years; // p holds address of years a = *p; // *p = value stored at address p, i.e. 20 printf("%d %d %d %d", years, a, *p, p); Output: 20 20 20 1245000
Background image of page 2
11/7/05 7 What’s it look like in memory 1245000: 1245004: 1245008: 1245012: 1245016: 1245020: 1245024: 1245028: int years, *p;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

W18-Pointers - Pointers 1 Pointers Yet another use for and...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online