1 / 19

Pointers

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.

pcanfield
Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers Ethan Cerami Fundamentals of Computer Science @1998 New York University

  2. Today • Computer Architecture • What’s a Pointer? • Declaring Pointers • Pointer Operators • & Address Operator • * Indirection Operator • Pointers and Call by Reference

  3. 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.

  4. 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.

  5. 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

  6. 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.”

  7. 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.

  8. 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

  9. 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

  10. 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

  11. 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.

  12. 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

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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); }

  18. 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

  19. Swapping Program Output Original Order: 4,5 After Swapping: 5,4

More Related