07+-+Array+Traversal

07+-+Array+Traversal - EECS 280 Programming and...

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

View Full Document Right Arrow Icon
Array Traversal EECS 280 Programming and Introductory Data Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
Pointer Review Working with addresses To declare a pointer to an int, we say: int *bar; Notice the “*”. It means “pointer-to”. We can assign values to a pointer, just as we can to any other variable: int foo; int *bar; bar = &foo; foo: bar: 1 ?
Background image of page 2
Pointer Review Using Pointers Once a pointer is created and assigned a value, we can use it to change the value of the object to which it points. We do this by saying: *bar = 2; This means “ change the value of the object to which bar points to 2 ”. The “*” here is the “dereference” operator. We sometimes say that dereferencing a pointer is the same as “following” it. The resultant environment is: int foo; int *bar; bar = &foo; foo: bar: 1 foo: bar: 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Pointers Passing to functions As with everything else, pointers can be arguments to functions. For example, suppose you want a function that adds one to an integer argument passed by reference. The prototype to that function is: void add_one(int *x); // MODIFIES: *x // EFFECTS: adds one to *x
Background image of page 4
Pointers Passing to functions void add_one(int *x) { *x = *x + 1; } If you called this in our example environment: add_one(bar); The variable bar is passed by value , but bar foo: bar: 2 x: 3
Background image of page 5

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

View Full DocumentRight Arrow Icon
Pointers Passing to functions void add_one(int *x) { *x = *x + 1; } If you called this in our example environment: add_one(bar); x is a copy of the pointer bar . Since they foo: bar: 3 x: int foo; int *bar; bar = &foo;
Background image of page 6
Pointers Passing to functions void add_one(int *x) { *x = *x + 1; } You can also call add_one without the “middleman” of bar , like so: add_one(&foo); foo: bar: 3 x:
Background image of page 7

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

View Full DocumentRight Arrow Icon
Pointers Question If you change the body of add_one to: void add_one(int *x) { x = x + 1; } …what happens?
Background image of page 8
Pointers Question This is a legal C++ statement, although it leaves x pointing to an undefined location. In other words, it increments the value of the pointer by one, not the thing it points to . However, what does it mean to increment a pointer “by one”? Pointer arithmetic is done based on units of the “referent type”. x=x+1 , tells the compiler that you want x to void add_one(int *x) { x = x + 1; } foo: bar: 3 bar++: ?
Background image of page 9

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

View Full DocumentRight Arrow Icon
Pointers Versus References This brings us to the essential differences between pointers and references: Both allow you to pass objects by reference. Pointers require some extra syntax at calling time ( ), in the argument list ( * ), and with each use ( * ); references only require extra syntax in the argument list ( ). You can change the object to which a pointer points using arithmetic/assignment, but you cannot change the object to which a reference refers.
Background image of page 10
Pointers Why us ‘em? There are (at least) two reasons to use
Background image of page 11

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

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

Page1 / 50

07+-+Array+Traversal - EECS 280 Programming and...

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

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