int Counter 7 int Pointer To Counter Pointer

  • Slides: 36
Download presentation

Δείκτες int Counter = 7; int *Pointer. To. Counter; Pointer. To. Counter = &Counter;

Δείκτες int Counter = 7; int *Pointer. To. Counter; Pointer. To. Counter = &Counter; Όνομα Counter count Θέση Μνήμης Τιμή … … 231000 7 … … 234000 231000 7 count. Ptr ΗΥ 150 – Προγραμματισμός count 7 Pointer. To. Counter 3 Κώστας Παναγιωτάκης

1 /* 2 Using the & and * operators */ 3 #include <stdio. h>

1 /* 2 Using the & and * operators */ 3 #include <stdio. h> 4 5 int main() The address of a is the value 6 { 7 int a; /* a is an integer */ of a. Ptr. 8 int *a. Ptr; /* a. Ptr is a pointer to an integer */ 9 The * operator returns an 10 a = 7; 11 a. Ptr = &a; /* a. Ptr set to address of a */ alias to what its operand 12 points to. a. Ptr points to a, 13 printf( "The address of a is %p" so *a. Ptr returns a. 14 "n. The value of a. Ptr is %p", &a, a. Ptr ); 15 16 printf( "nn. The value of a is %d" 17 "n. The value of *a. Ptr is %d", a, *a. Ptr ); 18 19 printf( "nn. Showing that * and & are inverses of " Notice how * and 20 "each other. n&*a. Ptr = %p" & are inverses 21 "n*&a. Ptr = %pn", &*a. Ptr, *&a. Ptr ); 22 23 return 0; 24 } The address of a is 0012 FF 88 The value of a. Ptr is 0012 FF 88 The value of a is 7 The value of *a. Ptr is 7 Proving that * and & are complements of each other. &*a. Ptr = 0012 FF 88 *&a. Ptr = 0012 FF 88 ΗΥ 150 – Προγραμματισμός Κώστας Παναγιωτάκης

1 /* 2 Notice that the function prototype Cube a variable using call-by-reference 3

1 /* 2 Notice that the function prototype Cube a variable using call-by-reference 3 with a pointer argument */ takes a pointer to an integer (int *). 4 5 #include <stdio. h> 6 Notice how the address of void cube. By. Reference( int * ); /* prototype */ number is given - cube. By. Reference expects a int main() pointer (an address of a variable). 7 8 9 10 { 11 int number = 5; 12 13 printf( "The original value of number is %d", number ); 14 cube. By. Reference( &number ); 15 printf( "n. The new value of number is %dn", number ); 16 17 return 0; Inside cube. By. Reference, *n. Ptr is used (*n. Ptr is number). 18 } 19 20 void cube. By. Reference( int *n. Ptr ) 21 { 22 *n. Ptr = *n. Ptr * *n. Ptr; /* cube number in main */ 23 } The original value of number is 5 The new value of number is 125 ΗΥ 150 – Προγραμματισμός Κώστας Παναγιωτάκης

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /* Fig. 7. 15: fig 07_15. c This program puts values into an array, sorts the values into ascending order, and prints the resulting array. */ #include <stdio. h> #define SIZE 10 void bubble. Sort( int *, const int ); void swap( int *, int * ); int main() { int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int i; Bubblesort gets passed the address of array elements (pointers). The name of an array is a pointer. printf( "Data items in original ordern" ); for ( i = 0; i < SIZE; i++ ) printf( "%4 d", a[ i ] ); bubble. Sort( a, SIZE ); /* sort the array */ printf( "n. Data items in ascending ordern" ); for ( i = 0; i < SIZE; i++ ) printf( "%4 d", a[ i ] ); printf( "n" ); return 0; } void bubble. Sort( int *array, const int size ) ΗΥ 150 – Προγραμματισμός { Κώστας Παναγιωτάκης

33 int pass, j; 34 for ( pass = 0; pass < size -

33 int pass, j; 34 for ( pass = 0; pass < size - 1; pass++ ) 35 36 for ( j = 0; j < size - 1; j++ ) 37 38 if ( array[ j ] > array[ j + 1 ] ) 39 swap( &array[ j ], &array[ j + 1 ] ); 40 } 41 42 void swap( int *element 1 Ptr, int *element 2 Ptr ) 43 { 44 int hold = *element 1 Ptr; 45 *element 1 Ptr = *element 2 Ptr; 46 *element 2 Ptr = hold; 47 } Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 ΗΥ 150 – Προγραμματισμός Κώστας Παναγιωτάκης

Παράδειγμα - ptrarithm 2. c #include <stdio. h> int main() { int n[10] =

Παράδειγμα - ptrarithm 2. c #include <stdio. h> int main() { int n[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 1}, *p, c; // First way to traverse the array printf("First way to index the array. n"); for (c=0; c < 10; c++) printf("n[%d] = %dn", c, n[c]); printf("n. Second way to index the array, through pointer arithmetic. n"); for (c=0; c < 10; c++) printf("n[%d] = %dn", c, *(n+c)); printf("n. Third way to index the array, through pointers arithmetic. n"); // We set the pointer to the beginning of the array p = n; for (c=0; c < 10; c++) printf("n[%d] = %dn", c, *(p+c)); printf("n. Fourth way to index the array, through pointers arithmetic. n"); p = n; for (c=0 ; c < 10; c++) printf("n[%d] = %d, %dn", c, *(p++), p); } printf("Checkn"); return 0; ΗΥ 150 – Προγραμματισμός 18 Κώστας Παναγιωτάκης

Παραδείγματα – types. c, types 2. c //types. c #include <stdio. h> int main()

Παραδείγματα – types. c, types 2. c //types. c #include <stdio. h> int main() { int *a; double *b; float *c; float (*d)[10]; float da[10]; printf("Size printf("Size of of of a = %dn", sizeof(a)); b = %dn", sizeof(b)); c = %dn", sizeof(c)); d = %dn", sizeof(d)); *d = %dn", sizeof(*d)); da = %dn", sizeof(da)); //types 2. c #include <stdio. h> int main() { int *a, n, array[10]; double *b; // Doing pointer arithmetic on a as an int * a = array; for (n=0; n < 10; n++) printf("Address of a is %dn", a++); // // return 0; } } ΗΥ 150 – Προγραμματισμός putchar('n'); Doing pointer arithmetic on b as an double * Notice that if I try to access the *b I may get a segmentation fault here, 'cause b points out of the array b = (double *)array; for (n=0; n < 10; n++) printf("Address of b is %dn", b++); return 0; 21 Κώστας Παναγιωτάκης

1 /* 2 Attempting to modify a constant pointer to 3 non-constant data */

1 /* 2 Attempting to modify a constant pointer to 3 non-constant data */ 4 5 #include <stdio. h> 6 7 int main() 8 { 9 int x, y; 10 Changing *ptr is allowed – x is not a constant. 11 int * const ptr = &x; /* ptr is a constant pointer to an 12 integer. An integer can be modified 13 through ptr, but ptr always points 14 to the same memory location. */ 15 *ptr = 7; 16 ptr = &y; 17 Changing ptr is an error – ptr is a constant pointer. 18 return 0; 19 } Error E 2024 FIG 07_13. c 16: Cannot modify a const object in function main *** 1 errors in Compile *** ΗΥ 150 – Προγραμματισμός Κώστας Παναγιωτάκης

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio. h> #include <stdlib. h> #include <time. h> void shuffle( int [][ 13 ] ); void deal( const int [][ 13 ], const char *[] ); int main() { const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; int deck[ 4 ][ 13 ] = { 0 }; srand( time( 0 ) ); shuffle( deck ); deal( deck, face, suit ); return 0; } void shuffle( int w. Deck[][ 13 ] ) { int row, column, card; ΗΥ 150 – Προγραμματισμός for ( card = 1; card <= 52; card++ ) { Κώστας Παναγιωτάκης

33 do { 34 row = rand() % 4; 35 column = rand() %

33 do { 34 row = rand() % 4; 35 column = rand() % 13; 36 } while( w. Deck[ row ][ column ] != 0 ); 37 The numbers 1 -52 are randomly placed into the deck array showing the position of card w. Deck[row][column]. 38 w. Deck[ row ][ column ] = card; 39 } 40 } 41 42 void deal( const int w. Deck[][ 13 ], const char *w. Face[], 43 const char *w. Suit[] ) 44 { 45 int card, row, column; 46 47 for ( card = 1; card <= 52; card++ ) 48 49 for ( row = 0; row <= 3; row++ ) 50 Searches deck for the card number, then prints the face and suit. 51 for ( column = 0; column <= 12; column++ ) 52 53 if ( w. Deck[ row ][ column ] == card ) 54 printf( "%5 s of %-8 s%c", 55 w. Face[ column ], w. Suit[ row ], 56 card % 2 == 0 ? 'n' : 't' ); 57 } card % 2 == 0 ? 'n' : 't' ); ΗΥ 150 – Προγραμματισμός 57 } Κώστας Παναγιωτάκης

 Six of Clubs Seven of Diamonds Ace of Spades Ace of Diamonds Ace

Six of Clubs Seven of Diamonds Ace of Spades Ace of Diamonds Ace of Hearts Queen of Diamonds Queen of Clubs Seven of Hearts Ten of Hearts Deuce of Clubs Ten of Spades Three of Spades Ten of Diamonds Four of Spades Four of Diamonds Ten of Clubs Six of Diamonds Six of Spades Eight of Hearts Three of Diamonds Nine of Hearts Three of Hearts Deuce of Spades Six of Hearts Five of Clubs Eight of Clubs Deuce of Diamonds Eight of Spades Five of Spades King of Clubs King of Diamonds Jack of Spades Deuce of Hearts Queen of Hearts Ace of Clubs King of Spades Three of Clubs King of Hearts Nine of Clubs Nine of Spades Four of Hearts Queen of Spades Eight of Diamonds Nine of Diamonds Jack of Diamonds Seven of Clubs Five of Hearts Five of Diamonds Four of Clubs Jack of Hearts Jack of Clubs Seven of Spades ΗΥ 150 – Προγραμματισμός Κώστας Παναγιωτάκης

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdio. h> #define SIZE 10 void bubble( int [], const int, int (*)( int, int ) ); int ascending( int, int ); int descending( int, int ); Notice the function pointer parameter. int main() { int order, counter, a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; printf( "Enter 1 to sort in ascending order, n" "Enter 2 to sort in descending order: " ); scanf( "%d", &order ); printf( "n. Data items in original ordern" ); for ( counter = 0; counter < SIZE; counter++ ) printf( "%5 d", a[ counter ] ); if ( order == 1 ) { bubble( a, SIZE, &ascending ); printf( "n. Data items in ascending ordern" ); } else { bubble( a, SIZE, &descending ); printf( "n. Data items in descending ordern" ); ΗΥ 150 – Προγραμματισμός } Κώστας Παναγιωτάκης

33 34 35 36 37 38 39 40 41 42 43 44 45 46

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 for ( counter = 0; counter < SIZE; counter++ ) printf( "%5 d", a[ counter ] ); printf( "n" ); return 0; } void bubble( int work[], const int size, int (*compare)( int, int ) ) { int pass, count; ascending and descending return true or false. bubble calls swap if the function call returns true. void swap( int *, int * ); for ( pass = 1; pass < size; pass++ ) for ( count = 0; count < size - 1; count++ ) if ( (*compare)( work[ count ], work[ count + 1 ] ) ) swap( &work[ count ], &work[ count + 1 ] ); } void swap( int *element 1 Ptr, int *element 2 Ptr ) { int temp; temp = *element 1 Ptr; *element 1 Ptr = *element 2 Ptr; *element 2 Ptr = temp; ΗΥ 150 – Προγραμματισμός } Notice how function pointers are called using the dereferencing operator. The * is not required, but emphasizes that compare is a function pointer and not a function. Κώστας Παναγιωτάκης

65 int ascending( int a, int b ) 66 { 67 return b <

65 int ascending( int a, int b ) 66 { 67 return b < a; /* swap if b is less than a */ 68 } 69 70 int descending( int a, int b ) 71 { 72 return b > a; /* swap if b is greater than a */ 73 } Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in descending order 89 68 45 37 12 10 8 6 4 2 ΗΥ 150 – Προγραμματισμός Κώστας Παναγιωτάκης