Friday, January 12, 2007
description
Transcript of Friday, January 12, 2007
Friday, January 12, 2007
Parkinson's LawParkinson's Law
““Work expands so as to fill Work expands so as to fill the time available for its the time available for its
completion."completion."
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?
Pointers and Arrays
Output is:
0012FF6C 0012FF6C55 5526 26
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
Pointers and Arrays
int i1[4]={55,26,17,68};int i2[4]={11, 2, 53, 14};i2=i1; //NOT ALLOWEDWhy? /*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
Pointers and Arrays
int i1[4]={55,26,17,68};int i2[4]={11,2,53,14};
int *p1;p1 = i1;int *p2;p2 = p1; //this is ok
Pointers and Arrays
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*/
SELF TEST: Pointers
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?
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;
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;
Using subscripting and pointer notations with arrays
0x0012FEBC 0x0012FEBC5 50x0012FEBC 0x0012FEBC
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;
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
SELF TEST: Using subscripting and pointer notations with arrays
int i1[4]={55, 26, 17, 68};What is wrong with the following statement?i1++;
//The following is okint *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
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);
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
Pointers and Arrays
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
Pointer Arithmetic
Output:0x0012FF74 0x0012FF5C10 10.510 10.50x0012FF78 0x0012FF6420 20.511 11.50x0012FF7C 0x0012FF6C30 30.512 12.5
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
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<(*i)++; // value is incremented
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<(*i)++; // value is incremented
//prints 1
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<++(*i); // value is incremented
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<++(*i); // value is incremented
//prints 2
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<*(i++); // value is incremented
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<*(i++); // value is incremented
//prints 1
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<*(++i); // value is incremented
Pointer Arithmetic
int a[3]={1, 5, 9};int *i; i = a;
cout<<*(++i); // value is incremented
//prints 5
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);
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
Pointer Arithmetic
What is wrong here?
int* my_Ptr;*my_Ptr=32;
Self Test
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++; }
Comparing Pointers
int x[ ] = {1, 3, 5, 7, 9, 11};*x = *(x + *x) + *x;cout<<*x;
Pointers
int x[ ] = {1, 3, 5, 7, 9, 11};*x = *(x + *x) + *x;cout<<*x; // prints 4
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';
char pointers
0x0012FF54 We are testing char pointers0x0012FF54 We are testing char pointers10 10W W
char pointers
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!
Pointers Are Funny!!Pointers Are Funny!!Pirointers Are Funny!!Qiinters Are Funny!!inny!!y!!nters Are Funny!!e
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;
Make a memory drawing!
Self Test