Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers.

Similar presentations


Presentation on theme: "Pointers."— Presentation transcript:

1 Pointers

2 Getting the Address of a Variable
Each variable in program is stored at a unique address Use address operator & to get address of a variable: int num = -23; cout << &num; // prints address // in hexadecimal Related, sample programs from the text are noted. See pr10-01.cpp

3 Pointer Variables Pointer variable (pointer): variable that holds an address Can perform some tasks more easily with an address than by accessing memory via a symbolic name: Accessing unnamed memory locations Array manipulation

4 Pointer Variables Definition: Read as:
int *intptr; Read as: “intptr can hold the address of an int” Spacing in definition does not matter: int * intptr; int* intptr;

5 int num = 25; Pointer Variables Assignment: int *intptr;
intptr = &num; Memory layout: Can access num using intptr and indirection operator *: cout << intptr; // prints 0x4a00 cout << *intptr; // prints 25 num intptr 25 0x4a00 address of num: 0x4a00 See pr10-02.cpp, pr10-03.cpp and pr10-04.cpp

6 Indirection Operator (dereferencing)
Value of a variable pointed to by ptr Achieved by * operator int num = 25; int *intptr; intptr = &num; cout<<*intptr; //prints 25

7 Pointers & Arrays

8 Relationship Between Arrays and Pointers
Array name is starting address of array int a[] = {4, 7, 11}; cout << a; // displays 0x4a00 cout << a[0]; // displays 4 4 7 11 starting address of a: 0x4a00

9 Example int main () { int a[ ] = {4, 7, 11}; int *ptr = a; cout << *a<<endl; for(int i=0;i<3;i++) cout<<a[i]<<" "; for( i=0;i<3;i++) cout<<*(ptr+i)<<" "; return 0;}

10 Pointers in Expressions
Given: int a[]={4,7,11}; int *ptr = a; cout << *a; // displays 4 What is ptr + 1? cout << *(ptr+1); // displays 7 cout << *(ptr+2); // displays 11 Must use ( ) in expression

11 Array Access Array notation a[i] is equivalent to the pointer notation

12 Pointer Arithmetic

13 Pointer Arithmetic - Integers can be added to or subtracted from
Some arithmetic operators can be used with pointers: - Increment and decrement operators ++, -- - Integers can be added to or subtracted from pointers using the operators +, -, +=, and -= - One pointer can be subtracted from another by using the subtraction operator -

14 Pointer arithmetic Integer math operations can be used with pointers.
If you increment a pointer, it will be increased by the size of whatever it points to. int *ptr = a; *(ptr+2) *(ptr+4) *ptr a[0] a[1] a[2] a[3] a[4]

15 Pointer Arithmetic (++/--)
Assume the variable definitions int a[]={4,7,11}; int *ptr = a; Examples of use of ++ and –- ptr++; // points at 7 ptr--; // now points at 4 See pr10-09.cpp

16 Pointer Arithmetic (+)
Assume the variable definitions: int a[]={4,7,11}; int *ptr = a; Example of use of + to add an int to a pointer: cout << *(ptr + 2) This statement will print 11

17 Pointer Arithmetic (+=)
Assume the variable definitions: int a[]={4,7,11}; int *ptr = a; Example of use of +=: ptr = a; // points at 4 ptr += 2; // points at 11

18 This statement prints 2: the number of ints between ptr and a
Pointer Arithmetic Assume the variable definitions int a[] = {4,7,11}; int *ptr = a; Example of pointer subtraction ptr += 2; cout << ptr - a; This statement prints 2: the number of ints between ptr and a

19 Comparing Pointers Relational operators can be used to compare addresses in pointers Comparing addresses in pointers is not the same as comparing contents pointed at by pointers: if (ptr1 == ptr2) // compares // addresses if (*ptr1 == *ptr2) // compares // contents See pr10-10.cpp

20 Pointers & Functions

21 Pointers as Function Parameters
A pointer can be a parameter Works like a reference parameter to allow change to argument from within function A pointer parameter must be explicitly dereferenced to access the contents at that address

22 Pointers as Function Parameters
Requires: 1) asterisk * on parameter in prototype and heading void getNum(int *ptr); 2) asterisk * in body to dereference the pointer cin >> *ptr; 3) address as argument to the function getNum(&num);

23 Pointers as Function Parameters
void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = -3; swap(&num1, &num2); See pr10-11.cpp and pr10-12.cpp

24 Example (returning search key index)
int find(int *p, int key) { for(int i=0;i<10;i++) if (*p++==key) return i; return (-1); }

25 Contd… int main () { int a[]={1,12,45,63,18,14,6,9,10,7}; int num;
cout<<"Enter number to search\n"; cin>>num; int *ptr=a; int k=find(ptr,num); if (k!=-1) cout<<"Element is at index "<<k<<endl; else cout<<"Element does not exist in array\n"; return 0;}

26 Copying string using pointers
void copystr(char*, const char*); //prototype int main() { char* str1 = "Self-conquest is the greatest victory."; char str2[80]; copystr(str2, str1); cout << str2 << endl; return 0; } void copystr(char* dest, const char* src) while( *src ) *dest++ = *src++; *dest = '\0';

27 Strings as function arguments
void dispstr (char*); int main() { char str[] = "Idle people have the least leisure."; dispstr(str); return 0; } void dispstr(char* ps) while( *ps ) cout << *ps++; cout << endl;


Download ppt "Pointers."

Similar presentations


Ads by Google