Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Ethan Cerami Fundamentals of Computer New York University.

Similar presentations


Presentation on theme: "Pointers Ethan Cerami Fundamentals of Computer New York University."— Presentation transcript:

1

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

3 Today n Computer Architecture n What’s a Pointer? n Declaring Pointers n Pointer Operators u & Address Operator u * Indirection Operator n Pointers and Call by Reference

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

5 RAM (Random Access Memory) n Every spot in memory has a unique address. n Declaring Variables: When you declare a variable, the operating system allocates a specific spot in memory for that variable. n For example, suppose you have int x = 5;... 1000 5 int x is placed at location 1005.

6 I. What’s a Pointer? n Pointer: contains the address of another variable. n For example, you could have two variables: x, xPtr. n Here, we say that xPtr “points to” x. n Or xPtr contains the address of x. x 5 xPtr

7 II. Declaring Pointers n data_type *var_name; n Pointers can point to any data type: int, char, float, double, long, etc. n Examples: u int *xPtr; F “xPtr is a pointer to type int.” u float *salesPtr; F “salesPtr is a pointer to type float.” * Indicates that this is a pointer.

8 III. Pointer Operators n Now we know how to declare pointers, but to use them you must understand the two pointer operators: n & Address Operator. Returns the address of a variable. n * Indirection Operator. Returns the value referenced by a pointer.

9 Pointer Operators in Action n int x = 5; n int *xPtr = NULL; xPtr points to Nothing 01234 x is located at address: 2 5 xPtr is located at address: 0 NULL

10 Using the Address & Operator n Now, you can apply the Address & operator. n &x Determines the address of x xPtr = &x; xPtr now points to x 01234 xxPtr 5 2

11 Using the * Indirection Operator n If you want to determine the value referenced by a pointer, you apply the * or indirection operator. n printf (“%d”, *xPtr);This prints “5” 01234 xxPtr 5 2

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

13 Example (Cont.) n aPtr “points” to a. aPtra 7 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

14 IV. Call by Reference n Call by Value: u When you pass a variable, you pass a copy of the variable. u Changes to the copy do not affect the original value. n Call by Reference: u When you pass a variable, you pass a reference (pointer) to the original variable. u Changes to the reference (pointer) do affect the original variable.

15 Limitations of Call by Value n You can only return a single value from a function. n So, what happens if you want to return two values, three values, more? n The only way around this is to use pointers.

16 Example n Suppose you want to create a simple function that simply swaps two numbers. n For example, you give it the numbers: 4,5. And the function returns 5,4. n This is particularly useful for sorting programs, such as Bubble Sort. n Only problem: there is no way to do this with Call by Value, because you need to modify and return two values.

17 Pointers and Call by Reference n 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.

18 Swapping with Pointers n To use swap in a program: 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); } Instead of passing the actual values, we pass the addresses of the variables.

19 Swapping in Action n Within main: swap (&x, &y); n within swap: u element1Ptr now points to x u element2Ptr now points to y u temp = *element1Ptr; sets temp =5; u *element1Ptr = *element2Ptr;sets x = 4; u *element2Ptr = temp;sets y = 5; x 5 element1Ptry 4 element2Ptr Before Swap:

20 Swapping Program Output Original Order: 4,5 After Swapping: 5,4


Download ppt "Pointers Ethan Cerami Fundamentals of Computer New York University."

Similar presentations


Ads by Google