Fundamentals of Programming Lecture 15 Hamed Rasifard 1
Outline • Types of Pointers • Arrays of Pointers • Multiple Indirection • Initializing Pointers • Pointers to Functions • Dynamic Memory Allocation 2
Types of Pointers • Non-constant pointer to non-constant data The data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data items. • Non-constant pointer to constant data A non-constant pointer to constant data can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified. 3
Types of Pointers(Cont.) • Constant pointer to non-constant data A constant pointer to non-constant data always points to the same memory location, and the data at that location can be modified through the pointer. • Constant pointer to constant data Such a pointer always points to the same memory location, and the data at that memory location cannot be modified. 4
Non-constant Pointer to Non-constant Data #include <stdio.h> #include <ctype.h> void convertToUppercase( char *sPtr ); /* prototype */ int main( void ) { char string[] = "characters and $32.98"; /* initialize char array */ printf( "The string before conversion is: %s", string ); convertToUppercase( string ); printf( "\nThe string after conversion is: %s\n", string ); return 0; /* indicates successful termination */ } /* end main */ /* convert string to uppercase letters */ void convertToUppercase(char *sPtr ) { while ( *sPtr != '\0' ) { /* current character is not '\0' */ if ( islower( *sPtr ) ) { /* if character is lowercase, */ *sPtr = toupper( *sPtr ); /* convert to uppercase */ } /* end if */ ++sPtr; /* move sPtr to the next character */ } /* end while */ } /* end function convertToUppercase */ 5
Non-constant Pointer to Constant Data #include <stdio.h> void printCharacters( const char *sP); int main( void ) { /* initialize char array */ char string[] = "print characters of a string"; printf( "The string is:\n" ); printCharacters( string ); printf( "\n" ); return 0; /* indicates successful termination */ } /* end main */ /* sPtr cannot modify the character to which it points, i.e., sPtr is a "read-only" pointer */ void printCharacters( const char *sPtr ) { /* loop through entire string */ for ( ; *sPtr != '\0'; sPtr++ ) { /* no initialization */ printf( "%c", *sPtr ); } /* end for */ } /* end function printCharacters */ 6
Non-constant Pointer to Non-constant Data #include <stdio.h> int main( void ) { int x; /* define x */ int y; /* define y */ /* ptr is a constant pointer to an integer that can be modified through ptr, but ptr always points to the same memory location */ int * const ptr = &x; *ptr = 7; /* allowed: *ptr is not const */ ptr = &y; /* error: ptr is const; cannot assign new address */ return 0; /* indicates successful termination */ } /* end main */ 7
Constant Pointer to Constant Data #include <stdio.h> int main( void ) { int x = 5; /* initialize x */ int y; /* define y */ /* ptr is a constant pointer to a constant integer. ptr always points to the same location; the integer at that location cannot be modified */ const int *const ptr = &x; printf( "%d\n", *ptr ); *ptr = 7; /* error: *ptr is const; cannot assign new value */ ptr = &y; /* error: ptr is const; cannot assign new address */ return 0; /* indicates successful termination */ } /* end main */ 8
Arrays of Pointers • Pointers can be arrayed like any other data type. • Declaration: Pointed_type *Array_name[Size] int *a[20]; • If you want to pass an array of pointers into a function, simply call the function with the array name without any subscripts. void display_array(int *q[]) { /*...*/ } 9
Multiple Indirection • It is possible to have a pointer point to another pointer that points to the target value. • his situation is called multiple indirection, or pointers to pointers. 10
Accessing a Target Value via Multiple indirection • To access the target #include <stdio.h> value indirectly pointed int main(void) to by a pointer to a { pointer, you must apply int x, *p, **q; the asterisk operator x = 10; twice. p = &x; q = &p; printf("%d", **q); /* print the value of x */ return 0; } 11
Initializing Pointers • If you try to use the pointer before giving it a valid value, you will probably crash your program • A pointer that does not currently point to a valid memory location is given the value null. • Null is used because C guarantees that no object will exist at the null address. • Any pointer that is null implies that it points to nothing and should not be used. 12
Initializing Pointers(Cont.) • One way to give a pointer a null value is to assign zero to it: char *p = 0; • Also. NULL macro could be used: p = NULL; • The assignment through NULL pointer causes an assignment at 0, which will usually cause a program crash. int *p = 0; *p = 10; /* wrong! */ 13
Pointers to Functions • A function has a physical location in memory that can be assigned to a pointer. • This address is the entry point of the function and it is the address used when the function is called. • Once a pointer points to a function, the function can be called through that pointer. • Function pointers also allow functions to be passed as arguments to other functions. 14
Comparing Two Strings #include <stdio.h> #include <string.h> void check(char *a, char *b, int (*cmp)(const char *, const char *)); int main(void) { char s1[80], s2[80]; int (*p)(const char *, const char *); /* function pointer */ p = strcmp; /* assign address of strcmp to p */ printf("Enter two strings.\n"); gets(s1); gets(s2); check(s1, s2, p); /* pass address of strcmp via p */ return 0; } void check(char *a, char *b, int (*cmp) (const char *, const char *)) { printf(''Testing for equality.\n"); if(!(*cmp)(a, b)) printf("Equal"); else printf("Not Equal"); } 15
Comparing Two Input #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> void check(char *a, char *b, int (*cmp)(const char *, const char *)); int compvalues(const char *a, const char *b); int main(void) { char s1[80], s2[80]; printf ("Enter two values or two strings.\n"); gets (s1); gets(s2); if(isdigit(*sl)) { printf(''Testing values for equality.\n"); check(s1, s2, compvalues); } else { printf("Testing strings for equality.\n"); check(s1, s2, strcmp); } return 0; } 16
Comparing Two Input(Cont.) void check(char *a, char *b, int (*cmp)(const char *, const char *)) { if(!(*cmp)(a, b)) printf("Equal"); else printf("Not Equal"); } int compvalues(const char *a, const char *b) { if(atoi(a)==atoi(b)) return 0; else return 1; } 17
Dynamic Memory Allocation • Some programs need to allocate memory spaces during execution. • In C language, it is done through the standard library function malloc(). • If there is not enough available memory to satisfy the malloc( ) request, an allocation failure occurs and malloc( ) returns a null. • The free( ) function is the opposite of malloc( ) in that it returns previously allocated memory to the system. 18
Dynamically Allocated Arrays • Sometimes you will want to allocate memory using malloc( ), but operate on that memory as if it were an array, using array indexing. /* Allocate space for a string dynamically, request user input, and then print the string backwards. */ #include <stdlib.h> #include <stdio.h> #include <string.h> int main(void) { char *s; register int t; s = malloc(80); if(!s) { printf(''Memory request failed.\n"); exit (1); } gets(s); for(t=strlen(s)-l; t>=0; t--) putchar(s[t]); free(s); return 0; } 19
Recommend
More recommend