07-array-traversal.student

07-array-traversal.student - Last time: * Arrays (0-based,...

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

View Full Document Right Arrow Icon
Last time: * Arrays (0-based, indexed, passed by reference) * Array pitfalls (no one checks "the edges") * Discussion regarding loop invariants Today: * Pointers (name locations in memory.) * Pointer syntax T *p; // p is a pointer-to-T T q; // q is of type T p = &q; // Let p "point to" q q = *p; // Let q be the value "pointed to by" p * More on pointers as arguments * Pointer arithmetic * Pointers vs. references * Array-Pointer duality ++++++++++++++++++++++++++++++++++++++++++++++++++++ How are arrays physically represented in the machine? The answer is via *pointers*. A pointer is a variable type that explicitly "points to" or refers to some other object in the environment. It turns out that arrays and pointers are very closely related---it's the mechanism for sharing arrays "under the covers". ++++++++++++++++++++++++++++++++++++++++++++++++++++++ So far, we've been talking about environments: blobs of state with objects in them. However, the computer needs a way to concretely name each particular item of state, be it a variable or a procedure. Each of these entities exists somewhere in the process' *memory*. Memory is a linear sequence of storage locations numbered from 0 to a very large number. Locations that hold some sort of object belonging to the program are *valid*, others are not. The location of its object is called its *address*. Normally, we never care precisely what an object's address is; the important thing is that every object has one. This is true of functions as well as variables. However, sometimes we want to explicitly work with the address of an object. To do this, we use a "pointer". Just as we were able to create variables which were "arrays of T" for nearly any type T, we can also create variable of type "pointer to T":
Background image of page 1

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

View Full DocumentRight Arrow Icon
A "pointer-to-T" is a variable that can hold the address of some other object of type T. For example, to declare a pointer to an int, we would say: int *bar; Notice the "*"; that means "pointer-to". As is typically for type names, we read from inside out. So bar is a pointer to an int. We can assign values to bar, just as we can to any other variable. However, the type of values bar expects is "address of an int". Here is how we write this: 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." The environment we get when we do this is: +-------------------------+ | +----+ | | foo | 1 |<---------\ | +----+ | | | +----+ | | | bar | --------------/ | +----+ | +-------------------------+ Technically, foo has some address, e.g., 0x804240c0, and bar has this address as its value. We don't need to know this since the
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 / 8

07-array-traversal.student - Last time: * Arrays (0-based,...

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