Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 1 6. Pointers. Arrays & Pointers. Call by Reference with Pointers 6.1.

Similar presentations


Presentation on theme: "10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 1 6. Pointers. Arrays & Pointers. Call by Reference with Pointers 6.1."— Presentation transcript:

1 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 1 6. Pointers. Arrays & Pointers. Call by Reference with Pointers 6.1. Pointers 6.2. Pointers and Arrays 6.3. Call by Reference with Pointers 6.4. Dynamic Arrays

2 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 2 6.1. Pointers - declaration What is a pointer? Data object whose value is the address of another data object, and knows the type/size of the object it points toDeclaration No new data type is introduced (if this were attempted one would need a pointer type for each existing data type). Instead pointers are declared through the type of the data object the pointer points to preceded by an operator (*), called the indirection or dereferencing operator, to unambiguously signal a pointer variable is declared.Example: int * iPtr; Syntax: * ;

3 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 3 Pointers - initialization Example: int i; int * iPtr = & i; Syntax: * = & ; & address operator: expression & i has the value of the address of i type: integer pointer type: pointer to type-pointed-to

4 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 4 Pointers - assignment Example: iPtr = & i; Syntax: = & ; pointer iPtr is assigned the address of i

5 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 5 // Fig. 6.1a modified from Figure 5.4 D&D // Declaring and Initializing pointers, // Using the & and * operators int a; //a is an integer a = 7; int *aPtr=&a; //aPtr declared as pointer int, and initialized to point to a cout << "\n\nThe value of a is " << a << "\nThe value of *aPtr is " << *aPtr; cout << "The address of a is " << &a << "\nThe value of aPtr is " << aPtr; cout << "\n\nShowing that * and & are inverses of " << "each other.\n&*aPtr = " << &*aPtr << "\n*&aPtr = " << *&aPtr << endl; The value of a is 7 The value of *aPtr is 7 The address of a is 0x0012FF7C The value of aPtr is 0x0012FF7C Showing that * and & are inverses of each other. &*aPtr = 0x0012FF7C *&aPtr = 0x0012FF7C

6 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 6 // Fig. 6.1b: Using the & and * operators a = 10;//same as in declaration int a=10; aPtr = &a; // aPtr assigned the address of a //contrast to declaration int *aPtr=&a; style int* aPtr=&a; cout << "\n\nThe value of a is " << a << "\nThe value of *aPtr is " << *aPtr; cout << "The address of a is " << &a << "\nThe value of aPtr is " << aPtr; cout << "\n\nShowing that * and & are inverses of " << "each other.\n&*aPtr = " << &*aPtr << "\n*&aPtr = " << *&aPtr << endl; return 0; The value of a is 10 The value of *aPtr is 10 The address of a is 0x0012FF7C The value of aPtr is 0x0012FF7C Showing that * and & are inverses of each other. &*aPtr = 0x0012FF7C *&aPtr = 0x0012FF7C

7 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 7 6.2. Pointers and Arrays Two important facts: 1. The name of the array is the address of the first element of the array. point is the same as &point[0] Note: Note: the name of the array (point) is is a constant 2. Adding 1 to a pointer value is the same as adding the size of the type it points to. point+1 same as & point[1] point+2 same as & point[2]

8 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 8 Accessing array elements: Assume the following declarations: int a[SIZE], *aPtr; aPtr = a; //equivalent to aPtr = &a[0]; Equivalent notations for the 6th and (i+1)th elements, i.e. the elements with offset 5 and i respectively. Array Subscript: a[5], a[i] Array Offset: *(a+5), *(a+i) Pointer Subscript: aPtr[5], aPtr[i] Pointer Offset: *(aPtr+5), *(aPtr+i), a[0] a[1] a[2] … a[5] … a[i] …. a[SIZE-1] a  &a[0] +1 +2 +5 +i Offset from the address of the first array element

9 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 9 6.3. Call by Reference with Pointers Goal: Goal: Allow the function to change the actual arguments a) with reference argumentsb) with pointer arguments Implementation Implementation:

10 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 10 Call by Reference with References References: ( References: (D&D, Ch.3.17) A reference object is a synonym or alias of another object. They are declared by the type of which they are an alias, followed by & Reference objects must be initialized as they are only aliases, i.e. they do not define/declare a new object: Call by Reference with References - Example int i; int& iRef = i; // iRef is an alias of iDefinition: void swapWithRef(int& a, int& b) { int s; s=a; a=b; b=s; }Call: swapWithRef(x, y);

11 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 11 Call by Reference with Pointers Definition: void swapWithPtr(int* a, int* b) { int s; s=*a; *a=*b; *b=s; }Call: swapWithPtr(&x, &y);

12 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 12 // Fig. 6.2: Swap using call-by-reference // with reference and pointer arguments void swapWithRef( int &, int &); // prototype void swapWithPtr( int *, int *); // prototype int main() { int x=7, y=15; cout << "Initial values are: \n"; cout << "first=" << x << " second= "<< y << endl; cout << "Swapping with references: \n"; swapWithRef(x, y); cout << "first=" << x << " second= "<< y << endl; cout << "Swapping with pointers: \n"; swapWithPtr(&x, &y); cout << "first=" << x << " second= "<< y << endl; return 0; } Initial values are: first=7 second= 15 Swapping with references: first=15 second= 7 Swapping with pointers: first=7 second= 15

13 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 13 Pointer, Arrays, Call-By-Reference - Summary Pointers are data objects that have an address as a value, and are declared through the type they point to and the indirection operator *. The array name is the address of the first element of the array. Adding 1 (or some integer i ) to a pointer means adding the size (or i times the size) of the type it points to. Call-by-reference provides functions with a mechanism for making changes in the calling environment. Call-by-reference can be implemented using either reference parameters or pointer parameters.

14 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 14 6. 4. Dynamic Arrays Goal: Goal: Allocate array memory at run time Implementation: function new() returns a pointer to a memory location at which the appropriate amount of memory for the array is allocated file: arrays-Dynamic.cpp

15 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 15 Dynamic Arrays: Examples, Declaration 1. int* colors=new int [10]; 2. double* stress=new double[SIZE];Syntax: * = new [ ] ; pointer to 1 st element returns pointer to array of with elements as determined by integer-expression array size

16 10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 16 Example: Decide on Array Size at Run Time //array size entered at run time int size_B; cout<<"Enter size of dynamic array: "; cin>>size_B; double* b=new double[size_B]; for(int i=0; i<size_B; i++) b[i]=(i+1)*10; //array size computed at run time int j=3, k=6; int* c=new int[j*k]; int d[j*k]; //ILLEGAL


Download ppt "10/20/08MET CS 563 - Fall 2008 6. Pointers, Arrays & Pointers. Call by Reference 1 6. Pointers. Arrays & Pointers. Call by Reference with Pointers 6.1."

Similar presentations


Ads by Google