07+-+Array+Traversal

07+-+Array+Traversal - 9/1/10 Pointer Review Working with...

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

View Full Document Right Arrow Icon
9/1/10 1 Array Traversal EECS 280 Programming and Introductory Data Structures 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 = 1; The symbol “&” means “address-of”. So, this statement says that bar is a pointer to an integer, initialized to the address of foo ”. foo: bar: 1 ? 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 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 And you'd write it as: void add_one(int *x) { *x = *x + 1; } 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 itself happens to be a pointer . So, the usual function calling rules apply: Create a new frame Evaluate the actual arguments in the calling environment Copy the values to the storage devoted to the formals Execute foo: bar: 2 x: 3 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 both have the same value (that value is the address of foo ), they both point to the same thing. foo: bar: 3 x: int foo; int *bar; bar = &foo;
Background image of page 1

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

View Full DocumentRight Arrow Icon
9/1/10 2 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); This says “take the address of foo , and pass it as the actual argument”. foo: bar: 3 x: Pointers Question If you change the body of add_one to: void add_one(int *x) { x = x + 1; } …what happens? 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 point to the “next” int.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 9

07+-+Array+Traversal - 9/1/10 Pointer Review Working with...

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

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