Pointers
Pointers
ITP 2024
Pointers
• What?
• Why?
• How?
IIT Hyderabad
10/9/24 3
Why Pointers?
Memory efficiency: Pointers can handle large data efficiently. Passing large structures or arrays
by pointer instead of value prevents the overhead of copying entire blocks of memory (allows
pass-by-reference).
Memory management: Pointers are crucial for dynamically allocating memory at runtime.
Useful for creating dynamic data structures.
Manipulating Arrays and Strings: Pointers can be used to access array elements directly.
Pointer arithmetic allows you to traverse arrays efficiently.
Function Pointers: Pointers can be used to point to functions, allowing functions to be passed
as arguments, stored in arrays, or dynamically assigned at runtime.
• The * is associated with the variable. The * doesn’t distribute to each var.
• int *a, b; // a is a pointer variable, b is a regular variable
• Int *c, *d; // Both c and d are pointers
Function cubeByReference is
passed an address, which can be the
value of a pointer variable
• Global variables usually violate the principle of least privilege and can
lead to poor software engineering. Global variables should be used
only to represent truly shared resources, such as the time of day.
Pointer/offset notation
Example-1
Pointers:
55 {
56 int pass; /* pass counter */
57
Example-1
int count; /* comparison counter */
58
59 void swap( int *element1Ptr, int *element2ptr ); /* prototype */
60
61 /* loop to control passes */
62 for ( pass = 1; pass < size; pass++ ) {
63
64 /* loop to control number of comparisons per pass */
65 for ( count = 0; count < size - 1; count++ ) {
66
67 /* if adjacent elements are out of order, swap them */
68 if ( (*compare)( work[ count ], work[ count + 1 ] ) ) {
69 swap( &work[ count ], &work[ count + 1 ] );
70 } /* end if */
71
Note that what the program
72 } /* end for */
73 considers “out of order” is
74 } /* end for */ dependent on the function
75 pointer that was passed to the
76 } /* end function bubble */
bubble function
77
Pointers:
81 {
82 int hold; /* temporary holding variable */
Example-1
83
84 hold = *element1Ptr;
85 *element1Ptr = *element2Ptr;
86 *element2Ptr = hold;
87 } /* end function swap */
88
89 /* determine whether elements are out of order for an ascending
90 order sort */
91 int ascending( int a, int b ) Passing the bubble function ascending
92 { will point the program here
93 return b < a; /* swap if b is less than a */
94
95 } /* end function ascending */
96
97 /* determine whether elements are out of order for a descending
98 order sort */
99 int descending( int a, int b )
Passing the bubble function descending
100 {
101 return b > a; /* swap if b is greater than a will
*/ point the program here
102
103 } /* end function descending */
Example-1 2 6 4 8 10 12
Data items in ascending order
89 68 45 37
2 4 6 8 10 12 37 45 68 89
Pointers: 4
5 /* prototypes */
Example-2 6
7
void function1( int a );
void function2( int b );
8 void function3( int c );
9
10 int main( void )
11 {
12 /* initialize array of 3 pointers to functions that each take an
13 int argument and return void */
14 void (*f[ 3 ])( int ) = { function1, function2, function3 };
15
16 int choice; /* variable to hold user's choice */ Array of pointers to functions
17
18 printf( "Enter a number between 0 and 2, 3 to end: " );
19 scanf( "%d", &choice );
20
Function
22 while ( choice >= 0 && choice < 3 ) {
23
Pointers:
24 /* invoke function at location choice in array f and pass
25 choice as an argument */
Function called is dependent on use
Example-2
26 (*f[ choice ])( choice );
27
28 printf( "Enter a number between 0 and 2, 3 to end: ");
29 scanf( "%d", &choice );
30 } /* end while */
31
32 printf( "Program execution completed.\n" );
33
34 return 0; /* indicates successful termination */
35
36 } /* end main */
37
38 void function1( int a )
39 {
40 printf( "You entered %d so function1 was called\n\n", a );
41 } /* end function1 */
42
43 void function2( int b )
44 {
45 printf( "You entered %d so function2 was called\n\n", b );
46 } /* end function2 */
Example-2
49 {
50 printf( "You entered %d so function3 was called\n\n", c );
51 } /* end function3 */
Example