07+-+Array+Traversal - 1 Array Traversa Ve 280 Programming...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

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

{[ snackBarMessage ]}

Page1 / 9

07+-+Array+Traversal - 1 Array Traversa Ve 280 Programming...

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