190 likes | 196 Views
Pointers. Ethan Cerami Fundamentals of Computer Science @1998 New York University. Today. Computer Architecture What’s a Pointer? Declaring Pointers Pointer Operators & Address Operator * Indirection Operator Pointers and Call by Reference. Computer Architecture.
E N D
Pointers Ethan Cerami Fundamentals of Computer Science @1998 New York University
Today • Computer Architecture • What’s a Pointer? • Declaring Pointers • Pointer Operators • & Address Operator • * Indirection Operator • Pointers and Call by Reference
Computer Architecture CPU: “Brains” of the Computer RAM: Short-term memory When you run your computer, RAM stores data for your operating system and all the applications you are running.
RAM (Random Access Memory) • Every spot in memory has a unique address. • Declaring Variables: When you declare a variable, the operating system allocates a specific spot in memory for that variable. • For example, suppose you have int x = 5; 5 ... ... 1000 int x is placed at location 1005.
I. What’s a Pointer? • Pointer: contains the address of another variable. • For example, you could have two variables: x, xPtr. • Here, we say that xPtr “points to” x. • Or xPtr contains the address of x. 5 xPtr x
II. Declaring Pointers * Indicates that this is a pointer. • data_type *var_name; • Pointers can point to any data type: int, char, float, double, long, etc. • Examples: • int *xPtr; • “xPtr is a pointer to type int.” • float *salesPtr; • “salesPtr is a pointer to type float.”
III. Pointer Operators • Now we know how to declare pointers, but to use them you must understand the two pointer operators: • & Address Operator. Returns the address of a variable. • * Indirection Operator. Returns the value referenced by a pointer.
Pointer Operators in Action • int x = 5; • int *xPtr = NULL; xPtr points to Nothing NULL 5 0 1 2 3 4 xPtr is located at address: 0 x is located at address: 2
Using the Address & Operator • Now, you can apply the Address & operator. • &x Determines the address of x xPtr = &x; xPtr now points to x 5 2 0 1 2 3 4 xPtr x
Using the * Indirection Operator • If you want to determine the value referenced by a pointer, you apply the * or indirection operator. • printf (“%d”, *xPtr); This prints “5” 5 2 0 1 2 3 4 xPtr x
Example main() { int a; /* a is an integer */ int *aPtr; /* aPtr is a pointer to an integer */ a = 7; aPtr = &a; /* aPtr set to address of a */ printf("The address of a is %p\n", &a); printf("The value of aPtr is %p\n\n", aPtr); printf("The value of a is %d\n", a); printf("The value of *aPtr is %d\n\n", *aPtr); return 0; } %p is the address format specifier. Formats large numbers in Hexadecimal format.
Example (Cont.) 7 • aPtr “points” to a. aPtr a Program Output: The address of a is 4F67:2240 The value of aPtr is 4F67:2240 The value of a is 7 The value of *aPtr is 7 These are Hexadecimal Values. %p is the format specifier for Pointer Addresses
IV. Call by Reference • Call by Value: • When you pass a variable, you pass a copy of the variable. • Changes to the copy do not affect the original value. • Call by Reference: • When you pass a variable, you pass a reference (pointer) to the original variable. • Changes to the reference (pointer) do affect the original variable.
Limitations of Call by Value • You can only return a single value from a function. • So, what happens if you want to return two values, three values, more? • The only way around this is to use pointers.
Example • Suppose you want to create a simple function that simply swaps two numbers. • For example, you give it the numbers: 4,5. And the function returns 5,4. • This is particularly useful for sorting programs, such as Bubble Sort. • Only problem: there is no way to do this with Call by Value, because you need to modify and return two values.
Pointers and Call by Reference • To get around this problem, we use pointers. void swap(int *element1Ptr, int *element2Ptr) { int temp; temp = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = temp; } * Indicates that we are passing pointers or addresses.
Swapping with Pointers • To use swap in a program: Instead of passing the actual values, we pass the addresses of the variables. main () { int x = 5; int y = 4; printf ("Original Order: %d,%d\n", x,y); swap (&x, &y); printf ("After Swapping: %d,%d", x,y); }
Swapping in Action • Within main: swap (&x, &y); • within swap: • element1Ptr now points to x • element2Ptr now points to y • temp = *element1Ptr; sets temp =5; • *element1Ptr = *element2Ptr; sets x = 4; • *element2Ptr = temp; sets y = 5; Before Swap: 5 4 element1Ptr x element2Ptr y
Swapping Program Output Original Order: 4,5 After Swapping: 5,4