1 / 59

# CS 192

CS 192. Lecture 12 Winter 2003 December 31, 2003 - January 1, 2004 Dr. Shafay Shamail. Pointer. Points to an item Holds the memory address of the item Has its own memory storage Occupies space in memory Hence can itself be accessed too Allows C/C++ to support dynamic memory allocation

Télécharger la présentation

## CS 192

E N D

### Presentation Transcript

1. CS 192 Lecture 12 Winter 2003 December 31, 2003 - January 1, 2004 Dr. Shafay Shamail

2. Pointer • Points to an item • Holds the memory address of the item • Has its own memory storage • Occupies space in memory • Hence can itself be accessed too • Allows C/C++ to support dynamic memory allocation • If x contains the address of y, x is said to “point to” y • Pointer variables are declared as type *var-name; e.g. int *p; pointer p contains the memory address of an int variable float *fl; points to a float variable

3. Assumptions • characters are one byte in length • integers are four bytes long • floats are four bytes long • doubles are eight bytes long

4. Pointer Operators & address of * value at address &a address of variable a *p contents of location pointed to by variable p

5. Pointer Operators • & (address operator) • a unary operator that returns the memory address of its operand. int balance = 350; int *balptr; balptr = &balance; • This address is the location of the variable in memory, it has nothing to do with the value of balance.

6. Pointer Operators • * (indirection operator) • Is the complement of &. • It is a unary operator that returns the value of variable located at address specified by its operand. int balance = 350; int *balptr; balptr = &balance; int value; value = *balptr; // what does value contain?

7. a b p ? 1 2 a b p 1 2 a b p 1 1 Pointers • int a=1, b=2, *p; • Picture in memory at this point: • p=&a; //p is assigned the address of a • b=*p; //b is assigned the value pointed to by p As p points to a, the statement b=*p is equivalent to b=a

8. Pointer Operators int main() { int balance; int *balptr; int value; balance = 3200; balptr = &balance; value = *balptr; cout << "balance is: " << value << '\n'; cout << "Memory address where balance is stored is: ” << balptr << endl; return 0; }

9. Output balance is: 3200 Memory address where balance is stored is: 0012FF7C (Note memory address may be different when you run it on your machine)

10. Base Type value = *balptr; • The compiler transfers the proper number of bytes according to base type. int *p; double f; // ... p = &f; // ERROR

11. Assigning Values Through a Pointer int *p; int x = 12; p=&x; cout << x << endl; //Assign a value to the location pointed to by p *p = 101; cout << x << endl; //what is value of x? cout << *p << endl; cout << p << endl; cout << &x << endl;

12. Assigning values through a pointer 12 101 101 0012FF78 0012FF78

13. Assigning Values Through a Pointer (*p)++;//increment value to the location pointed to by p int main() { int *p, num; p = &num; *p = 454; cout << num << ' '; (*p)++; /* parentheses are necessary because * operator has lower precedence than ++operator */ cout << num << ' '; (*p) - -; cout << num << '\n'; return 0; } //Output?

14. Assigning Values Through a Pointer //Output is 454 455 454

15. Pointer Arithmetic • There are only four arithmetic operators that can be used on pointers: ++, --, +, - • Let p1 be an integer pointer which contains the address 5000 • p1++; // now p1 will be 5004 • Each time p1 is incremented, it shall point to the next integer. • p1--; will cause p1 to be 4996 if initially it was 5000.

16. Pointer Arithmetic • Let p1 be a char pointer which contains the address 4000 • p1++; // now p1 will be 4001 • Each time p1 is incremented, it shall point to the next character. • p1--; will cause p1 to be 3999 if initially it was 4000. • Pointer of type other than char shall increase or decrease by length of base type.

17. Pointer Arithmetic • You cannot add two pointers • You can subtract two pointers (if they are of same base type). • Other than • addition or subtraction of a pointer and an integer, OR • csubtraction of two pointers, no other arithmetic operations can be performed on pointers. • You cannot add or subtract float or double values.

18. Pointer Arithmetic int main() { int *iptr, a; //which is pointer to int? which is int? double *fptr, b; int x; a = 10; b = 20; iptr = &a; fptr = &b; for ( x = 0; x < 10; x++) cout << iptr+x << " " << fptr+x << '\n'; return 0; }

19. Pointers and Arrays • In C++, there is a close relationship between pointers and arrays. • Two examples

20. Pointers and Arrays int main() { int *iptr; int iarray[4] = { 5, 6, 7, 8}; iptr = iarray; cout << iptr << " " << iarray << “\n”; cout << iptr[0] << " " << iarray[0] << “\n”; cout << *(iptr+1) << " " << iarray[1] <<“\n”; return 0; } //Output?

21. Pointers and Arrays Output is: 0012FF6C 0012FF6C 5 5 6 6

22. Pointers and Arrays int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bptr = b; // set bPtr to point to array b int *b2ptr = b+5; /* set b2Ptr to point to sixth element of array b */ cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr = " << *b2ptr << endl; cout << "(b2ptr - bptr) = " << (b2ptr - bptr) << endl;

23. Pointers and Arrays int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bptr = b; // set bPtr to point to array b int *b2ptr = b+5; /* set b2Ptr to point to sixth element of array b */ cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr = " << *b2ptr << endl; cout << "(b2ptr - bptr) = " << (b2ptr - bptr) << endl; Output is: b[5] = 60 *b2ptr = 60 (b2ptr - bptr) = 5

24. Pointers and Arrays int i1[4]={5,6,7,8}; int i2[4]={1,2,3,4}; i2=i1; //NOT ALLOWED Why? /*name of array is a constant that points to beginning of array*/ char str1[]=“i am a string”; char str2[]=“i am a string”; str1==str2; //WRONG way of string comaprison Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful

25. Pointers and Arrays inti1[4]={5,6,7,8}; int i2[4]={1,2,3,4}; int*ptr1=i1; int *ptr2; ptr2=ptr1; //this is ok

26. Pointers and Arrays int i1[4]={5,6,7,8}; What is wrong with the following statement? i1++; //The following is ok int *iptr=i1; iptr++; cout<<*iptr; //The following is ok *(i1+3) = 100; // This is OK because i1 has not changed

27. Using Subscripting and Pointer Notations with Arrays int b[] = {10, 20, 30, 40}; int *bptr = b; // set bptr to point to array b cout << "Array b printed with:" << endl << "Array subscript notation" << endl; for (int i = 0; i <= 3; i++) cout <<b[i] << endl; cout << "Pointer subscript notation" << endl; for (i = 0; i <= 3; i++) cout << bptr[i] << endl;

28. Using Subscripting and Pointer Notations with Arrays int offset; cout << "Pointer/offset notation where" << endl << "the pointer is the array name" << endl; for (offset = 0; offset <= 3; offset++) cout << *(b + offset) << endl; cout <<"Pointer/offset notation" << endl; for (offset = 0; offset <= 3; offset++) cout << *(bptr + offset) << endl;

29. Using Subscripting and Pointer Notations with Arrays int *iptr; int iarray[4] = {5,6,7,8}; iptr = iarray; cout << iptr << " " << iarray <<endl; cout << iptr[0] << " " << iarray[0]<< endl ; cout << &iptr[0] << " " << &iarray[0]<< endl;

30. Pointer Arithmetic • int main() • { • int a[3] = {10, 20, 30}; • double b[3] = {10.5, 20.5, 30.5}; • int *iptr; • double *fptr; • int x, size=3; • iptr = a; //name of array is a constant that points to beginning of array • fptr = b; • for(x=0; x<size; x++) • { • cout << iptr+x << " " << fptr+x << '\n'; • cout <<*( iptr+x) << " " <<*( fptr+x) << '\n'; • cout << *iptr+x << " " << *fptr+x << '\n'; • } • return 0; • } //pointer-add example

31. Pointer Arithmetic Output: 0x0012FF74 0x0012FF5C 10 10.5 10 10.5 0x0012FF78 0x0012FF64 20 20.5 11 11.5 0x0012FF7C 0x0012FF6C 30 30.5 12 12.5

32. Pointer Arithmetic int a[3] = {1, 2, 3}; int *iptr; iptr = a; //what is the difference between *iptr++ and (*iptr)++ and *(iptr++)? cout<<*iptr++; cout<<*++iptr; cout<<(*iptr)++; cout<<++(*iptr); cout<<*(iptr++); cout<<*(++iptr);

33. Pointer Arithmetic int a[3] = {1, 5, 9}; int *iptr; iptr = a; //what is the difference between *iptr++ and (*iptr)++ and *(iptr++)? Output: (looking at each statement independently) cout<<*iptr++; // 1 cout<<*++iptr; // 5 cout<<(*iptr)++; // 1 cout<<++(*iptr); // 2 cout<<*(iptr++); // 1 cout<<*(++iptr); // 5

34. Self Test: Pointer Arithmetic int a[3]={1, 5, 9}; int *iptr; iptr = a; Self Test: what is output if the statements are executed one after the other? //Be-careful is there array overrun? garbage values? cout<<*iptr++; cout<<*++iptr; cout<<(*iptr)++; cout<<++(*iptr); cout<<*(iptr++); cout<<*(++iptr);

35. Self Test-2 What is wrong here? int* myptr; *myptr=32;

36. Self Test What is output here? int x=235; int a[3]={1, 5, 9}; cout<<*(&x); cout<<*(&a[1]);

37. Pointers & Arrays • Is anything wrong with the following: int num[3]; int i; for(i=0; i<10; i++) { *num = i; // is this OK? num++; // is this OK? } • Can’t modify num. Can’t modify a pointer constant (an array’s name) • But this is ok: int num[3]= {0, 1, 2}; int *p = num; *(p++);

38. p a c b \0 Pointers & Strings • A string constant, like an array name, is treated by the compiler as a pointer char *p = “abc”; cout<<p<<endl<<p+1<<endl<<*p<<endl<<*(p+1); //output? • abc bc a b • pis assigned the base address of the character array“abc”. String printed till null character, as usual • Can we use such expressions: “abc”[1] and *(“abc” + 2) • Of course, outputis b and c respectively

39. Pointer Comparisons start • Pointers may be compared using relational operators (e.g. ==, <, >). • To be meaningful, pointers should be of same type int num[10]; int *start, *end; start = num; end = &num[9]; while(start!=end) { cout << "Enter a number: "; cin >> *start; start++; } start = num; /* reset the starting pointer */ while(start!=end) { cout << *start << ' '; start++; } … num end

40. Arrays of Pointers • Can sure do that like other data types e.g. int *zztop[10]; //array of 10 int pointers • Now, say int var; zztop[3]=&var; *zztop[3]; • Arrays of pointers to strings commonly used char *fortunes[] = { "Soon, you will come into some money.\n", "A new love will enter your life.\n", "You will live long and prosper.\n", "Now is a good time to invest for the future.\n", "A close friend will ask for a favor.\n" }; cout << fortunes[1] << endl << fortunes<<endl << *(fortunes) << endl << *(fortunes[2]) << endl; • Output: A new love will enter your life 0012FFC6 Soon, you will come into some money Y

41. Arrays of Pointers • Two dimensional string pointers e.g. C++ dictionary char *keyword[][2] = { "for", "for(initialization; condition; increment)", "if", "if(condition) ... else ...", "switch", "switch(value) { case-list }", "while", "while(condition) ...", // add the rest of the C++ keywords here "", "" // terminate the list with nulls }; int main() { char str[80]; int i; cout << "Enter keyword: "; cin >> str; for(i=0; *keyword[i][0]; i++) if(!strcmp(keyword[i][0], str)) cout << keyword[i][1]; return 0; }

42. Array of Strings vs. Array of Pointers • Spot the difference: • char movies[5][20] = {“Godfather”, “Maula Jatt”, “A Fish Called Wanda”, “Blade Runner”, “Spiderman”}; • char *movies[5] = {“Godfather”, “Maula Jatt”, “A Fish Called Wanda”, “Blade Runner”, “Spiderman”}; • In the first case, each column is 20 characters wide due to the longest movie name; wasted space • In the second case, the strings are placed contiguously in memory without wasting space; pointers in the array movie point to them

43. Dynamic Allocation The new operator int *x_ptr = new int; OR int *xptr; xptr = new int; //heap

44. Dynamic Allocation int *xptr=new int; *xptr = 73; int *x2ptr = new int; *x2ptr = 65;

45. Dynamic Allocation What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr; *x2ptr=65;

46. Dynamic Allocation int *xptr = new int; *xptr = 73; int *x2ptr; x2ptr = xptr; *x2ptr = 65;

47. Dynamic Allocation //What is wrong here? int *xptr = new int; *xptr = 73; int *x2ptr = new int; x2ptr = xptr; *x2ptr = 65; //memory leak

48. Dynamic Allocation int *myptr = new int(73); cout << *myptr; delete myptr;

49. Dynamic Allocation const int SIZE = 10; double *ptr = new double[SIZE]; /*10 element array*/ int i; for (i=0; i<SIZE; i++) { ptr[i] = 2.0*i; } for (i=0; i<SIZE; i++) { cout << ptr[i] << endl; } delete []ptr;

50. Char Pointers int *intPtr; int i[10]={10,11,12,13,14,15,16,17,18,19}; char *charPtr; char c[]="We are testing char pointers"; intPtr = i; charPtr = c; cout << i <<" "<< c << endl; cout << intPtr << " " << charPtr << '\n'; cout << *intPtr <<" "<< i[0] << '\n';

More Related