Download presentation
Presentation is loading. Please wait.
1
Friday, January 12, 2007 Parkinson's Law “Work expands so as to fill the time available for its completion."
2
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
3
Output is: 0012FF6C 55 26 Pointers and Arrays
4
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
5
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 Pointers and Arrays
6
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
7
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
8
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
9
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
10
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
11
0x0012FEBC 5 0x0012FEBC Using subscripting and pointer notations with arrays
12
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
13
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
14
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 Pointers and Arrays
15
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
16
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
17
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
18
Output: 0x0012FF74 0x0012FF5C 10 10.5 0x0012FF78 0x0012FF64 20 20.5 11 11.5 0x0012FF7C 0x0012FF6C 30 30.5 12 12.5 Pointer Arithmetic
19
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
20
int a[3]={1, 5, 9}; int *i; i = a; cout<<(*i)++; // value is incremented Pointer Arithmetic
21
int a[3]={1, 5, 9}; int *i; i = a; cout<<(*i)++; // value is incremented //prints 1 Pointer Arithmetic
22
int a[3]={1, 5, 9}; int *i; i = a; cout<<++(*i); // value is incremented Pointer Arithmetic
23
int a[3]={1, 5, 9}; int *i; i = a; cout<<++(*i); // value is incremented //prints 2 Pointer Arithmetic
24
int a[3]={1, 5, 9}; int *i; i = a; cout<<*(i++); // value is incremented Pointer Arithmetic
25
int a[3]={1, 5, 9}; int *i; i = a; cout<<*(i++); // value is incremented //prints 1 Pointer Arithmetic
26
int a[3]={1, 5, 9}; int *i; i = a; cout<<*(++i); // value is incremented Pointer Arithmetic
27
int a[3]={1, 5, 9}; int *i; i = a; cout<<*(++i); // value is incremented //prints 5 Pointer Arithmetic
28
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
29
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
30
What is wrong here? int* my_Ptr; *my_Ptr=32; Self Test
31
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
32
int x[ ] = {1, 3, 5, 7, 9, 11}; *x = *(x + *x) + *x; cout<<*x; Pointers
33
int x[ ] = {1, 3, 5, 7, 9, 11}; *x = *(x + *x) + *x; cout<<*x; // prints 4 Pointers
34
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
35
0x0012FF54 We are testing char pointers 10 W char pointers
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!! 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; Make a memory drawing! Self Test
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.