1 / 38

Friday, January 12, 2007

Friday, January 12, 2007. Parkinson's Law “Work expands so as to fill the time available for its completion.&quot;. Pointers and Arrays. int main() { int *i; int i_array[4]={55, 26, 17, 68}; i=i_array; cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; i_array &lt;&lt; “<br>”;

arista
Download Presentation

Friday, January 12, 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."

  2. Pointers and Arrays int main() { int *i; int i_array[4]={55, 26, 17, 68}; i=i_array; cout << i << " " << i_array << “\n”; cout << i[0] << " " << i_array[0] << “\n”; cout << *(i+1) << " " << i_array[1] <<“\n”; return 0; } //Output?

  3. Pointers and Arrays Output is: 0012FF6C 0012FF6C 55 55 26 26

  4. Pointers and Arrays int i1[4]={55,26,17,68}; int i2[4]={11, 2, 53, 14}; i2=i1; //NOT ALLOWED char str1[]=“i am a string”; char str2[]=“i am a string”; str1==str2; //WRONG Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful

  5. Pointers and Arrays int i1[4]={55,26,17,68}; int i2[4]={11, 2, 53, 14}; 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 comparison Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful

  6. Pointers and Arrays inti1[4]={55,26,17,68}; int i2[4]={11,2,53,14}; int *p1; p1 = i1; int *p2; p2 = p1; //this is ok

  7. SELF TEST: Pointers int *y_ptr, y; //y is of type int // y_ptr is pointer to int y=45; y_ptr=&y; int *y_ptr, *another_ptr, y=45; y_ptr=&y; another_ptr=&y; /*what is *another_ptr what is *y_ptr*/

  8. Using subscripting and pointer notations with arrays int b[] = {10, 20, 30, 40}; int *bPtr; bPtr = b; // set bPtr to point to array b cout << "Array b printed with:" << endl << "Array subscript notation" << endl; for (int i = 0; i < 4; i++) cout <<b[i] << endl; cout << "Pointer subscript notation" << endl; for (i = 0; i < 4; i++) cout << bPtr[i] << endl;//Another way to do this?

  9. 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 < 4; offset++) cout << *(b + offset) << endl; cout <<"Pointer/offset notation" << endl; for(offset = 0; offset < 4; offset++) cout << *(bPtr + offset) << endl;

  10. Using subscripting and pointer notations with arrays int *i; int int_array[4]={5,6,7,8}; i=int_array; cout << i << " " << int_array<<endl; cout << i[0] << " " << int_array[0]<< endl ; cout << &i[0] << " " << &int_array[0]<< endl;

  11. Using subscripting and pointer notations with arrays 0x0012FEBC 0x0012FEBC 5 5 0x0012FEBC 0x0012FEBC

  12. SELF TEST:Using subscripting and pointer notations with arrays int a[5]={12, 3, 45, 5, 8}; int *ptr; ptr=a; cout<<a[0]<<endl; cout<<ptr[0]<<endl; cout<<*(a+0)<<endl; cout<<*(ptr+0)<<endl; cout<<a[1]<<endl; cout<<ptr[1]<<endl; cout<<*(a+1)<<endl; cout<<*(ptr+1)<<endl;

  13. SELF TEST:Using subscripting and pointer notations with arrays int a[5]={12, 3, 45, 5, 8}; int *ptr; ptr=a; cout<<a[0]<<endl; //prints 12 cout<<ptr[0]<<endl; //prints 12 cout<<*(a+0)<<endl; //prints 12 cout<<*(ptr+0)<<endl; //prints 12 cout<<a[1]<<endl; //prints 3 cout<<ptr[1]<<endl; //prints 3 cout<<*(a+1)<<endl; //prints 3 cout<<*(ptr+1)<<endl; //prints 3

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

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

  16. Pointers and Arrays int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? 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

  17. Pointer Arithmetic int main() { int a[3]={10, 20, 30}; double b[3]={10.5, 20.5, 30.5}; int *i; double *f; int x, size=3; i = a; f = b; for(x=0; x<size; x++) { cout << i+x << " " << f+x << '\n'; cout <<*( i+x) << " " <<*( f+x) << '\n'; cout << *i+x << " " << *f+x << '\n'; } return 0; } //pointer-add example

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

  19. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; what is the difference between *i++ and (*i)++ and *(i++)? cout<<(*i)++; // display and then increment pointee cout<<++(*i); // increment pointee and then display cout<<*(i++); // display and then increment pointer cout<<*(++i); // increment pointer and then display

  20. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<(*i)++; // value is incremented

  21. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<(*i)++; // value is incremented //prints 1

  22. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<++(*i); // value is incremented

  23. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<++(*i); // value is incremented //prints 2

  24. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<*(i++); // value is incremented

  25. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<*(i++); // value is incremented //prints 1

  26. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<*(++i); // value is incremented

  27. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; cout<<*(++i); // value is incremented //prints 5

  28. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; What is output if the statement are executed one after the other? cout<<(*i)++; cout<<++(*i); cout<<*(i++); cout<<*(++i);

  29. Pointer Arithmetic int a[3]={1, 5, 9}; int *i; i = a; What is output if the statement are executed one after the other? cout<<(*i)++; // 1 cout<<++(*i); // 3 cout<<*(i++); // 3 cout<<*(++i); // 9

  30. Self Test What is wrong here? int* my_Ptr; *my_Ptr=32;

  31. Comparing Pointers 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++; }

  32. Pointers int x[ ] = {1, 3, 5, 7, 9, 11};*x = *(x + *x) + *x;cout<<*x;

  33. Pointers int x[ ] = {1, 3, 5, 7, 9, 11};*x = *(x + *x) + *x;cout<<*x; // prints 4

  34. 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'; cout << *charPtr <<" "<< c[0] << '\n';

  35. char pointers • 0x0012FF54 We are testing char pointers • 0x0012FF54 We are testing char pointers • 10 • W W

  36. char n[25] = "Pointers Are Funny!!"; char *name, *temp; name = n; temp = name+2; cout << n << endl; cout << name << endl; cout << *name << endl; cout << *(temp) << endl; cout << n[10] << endl; cout << ++name << endl; cout << ++(*n) << endl; cout << *(temp) << endl; cout << ++name << endl; cout << *name << endl; cout << n + 15 << endl; cout << name + 15 << endl; cout << ++name << endl; cout << name[2] << endl; Make a memory drawing!

  37. Pointers Are Funny!! Pointers Are Funny!! P i r ointers Are Funny!! Q i inters Are Funny!! i nny!! y!! nters Are Funny!! e

  38. char n[25] = "Pointers Are Funny!!"; char *name; name = n; cout << n << endl; cout << name << endl; cout << *name << endl; cout << *(name+2) << endl; cout << n[10] << endl; cout << ++name << endl; cout << ++(*n) << endl; cout << *(n+2) << endl; cout << ++name << endl; cout << *name << endl; cout << n + 15 << endl; cout << name + 15 << endl; cout << ++name << endl; cout << name[2] << endl; Self Test Make a memory drawing!

More Related