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

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

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

Unformatted text preview: Last time: * Arrays (0-based, indexed, passed by reference) * Array pitfalls (no one checks "the edges") 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 passed as arguments 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 passing 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. Think of it as a very long street. 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*. Thus, each variable has a unique address on the street. 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. Some objects are "small", and occupy only one "address" (one byte, on most machines.) Others are "big" and occupy more than one "address"---more than one byte. Usually, we don't have to worry about this. For example, in C++, to talk about the integer foo: int foo; we only need to write "foo". The compiler handles the following issues for us so we don't have to worry about it: 1: where to allocate space for foo 2: how to access the contents of foo when foo is evaluated 3: how to change the value of foo when foo is assigned to. However, sometimes we want to explicitly work with the address of an object---or, perhaps a collection of objects. 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": 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....
View Full 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.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