Pointers Getting the Address of a Variable Each

Pointers

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 << # // prints address // in hexadecimal

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

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

Pointer Variables � Assignment: int num = 25; int *intptr; intptr = # � Memory layout: num intptr 25 0 x 4 a 00 address of num: 0 x 4 a 00 Can access num using intptr and indirection operator *: cout << intptr; // prints 0 x 4 a 00 cout << *intptr; // prints 25

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

Pointers & Arrays

Relationship Between Arrays and Pointers � Array name is starting address of array int a[] = {4, 7, 11}; 4 7 11 starting address of a: 0 x 4 a 00 cout << a; // displays 0 x 4 a 00 cout << a[0]; // displays 4
![Example int main () { int a[ ] = {4, 7, 11}; int *ptr Example int main () { int a[ ] = {4, 7, 11}; int *ptr](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-9.jpg)
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; }
![Pointers in Expressions Given: int a[]={4, 7, 11}; int *ptr = a; cout << Pointers in Expressions Given: int a[]={4, 7, 11}; int *ptr = a; cout <<](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-10.jpg)
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
![Array Access � Array notation a[i] is equivalent to the pointer notation *(a + Array Access � Array notation a[i] is equivalent to the pointer notation *(a +](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-11.jpg)
Array Access � Array notation a[i] is equivalent to the pointer notation *(a + i)

Pointer Arithmetic

Pointer Arithmetic � 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 -

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 a[0] a[1] a[2] a[3] *(ptr+4) a[4]
![Pointer Arithmetic (++/--) � Assume the variable definitions int a[]={4, 7, 11}; int *ptr Pointer Arithmetic (++/--) � Assume the variable definitions int a[]={4, 7, 11}; int *ptr](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-15.jpg)
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
![Pointer Arithmetic (+) • Assume the variable definitions: int a[]={4, 7, 11}; int *ptr Pointer Arithmetic (+) • Assume the variable definitions: int a[]={4, 7, 11}; int *ptr](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-16.jpg)
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
![Pointer Arithmetic (+=) • Assume the variable definitions: int a[]={4, 7, 11}; int *ptr Pointer Arithmetic (+=) • Assume the variable definitions: int a[]={4, 7, 11}; int *ptr](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-17.jpg)
Pointer Arithmetic (+=) • Assume the variable definitions: int a[]={4, 7, 11}; int *ptr = a; • Example of use of +=: ptr = a; ptr += 2; // points at 4 // points at 11
![Pointer Arithmetic � Assume the variable definitions int a[] = {4, 7, 11}; int Pointer Arithmetic � Assume the variable definitions int a[] = {4, 7, 11}; int](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-18.jpg)
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

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 (ptr 1 == ptr 2) // compares // addresses if (*ptr 1 == *ptr 2) // compares // contents

Pointers & Functions

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

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

Pointers as Function Parameters void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int num 1 = 2, num 2 = -3; swap(&num 1, &num 2);

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); }
![Contd… int main () { int a[]={1, 12, 45, 63, 18, 14, 6, 9, Contd… int main () { int a[]={1, 12, 45, 63, 18, 14, 6, 9,](http://slidetodoc.com/presentation_image_h2/6ae433ebfe492916b21cd6a25886d118/image-25.jpg)
Contd… int main () { int a[]={1, 12, 45, 63, 18, 14, 6, 9, 10, 7}; int num; cout<<"Enter number to searchn"; 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 arrayn"; return 0; }

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