07+-+Array+Traversal

07+-+Array+Traversal - 9/27/2009 Pointer Review Working...

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

View Full Document Right Arrow Icon
9/27/2009 1 Array Traversal EECS 280 Programming and Introductory Data Structures Pointer Review Working with addresses c To declare a pointer to an int, we say: int *bar; c Notice the “*”. It means “pointer-to”. c We can assign values to a pointer, just as we can to any other variable: int foo; int *bar; bar = &foo; foo = 1; c 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 c 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; c This means “ change the value of the object to which bar points to 2 ”. c The “*” here is the “dereference” operator. c 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 c 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. c The prototype to that function is: void add_one(int *x); // MODIFIES: *x // EFFECTS: adds one to *x c 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; } c If you called this in our example environment: add_one(bar); c The variable bar is passed by value , but bar itself happens to be a pointer . So, the usual function calling rules apply: c Create a new frame c Evaluate the actual arguments in the calling environment c Copy the values to the storage devoted to the formals c Execute foo: bar: 2 x: 3 Pointers Passing to functions void add_one(int *x) { *x = *x + 1; } c If you called this in our example environment: add_one(bar); c 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/27/2009 2 Pointers Passing to functions void add_one(int *x) { *x = *x + 1; } c You can also call add_one without the “middleman” of bar , like so: add_one(&foo); c This says “take the address of foo , and pass it as the actual argument”. foo: bar: 3 x: Pointers Question c If you change the body of add_one to: void add_one(int *x) { x = x + 1; } c …what happens? Pointers Question c 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 . c However, what does it mean to increment a pointer “by one”? c
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.

Page1 / 9

07+-+Array+Traversal - 9/27/2009 Pointer Review Working...

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