Fundamentals of Programming
Lecture 15 Hamed Rasifard
1
Fundamentals of Programming Lecture 15 Hamed Rasifard 1 Outline - - PowerPoint PPT Presentation
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
Lecture 15 Hamed Rasifard
1
2
The data can be modified through the dereferenced pointer, and the pointer can be modified to point to
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
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.
Such a pointer always points to the same memory location, and the data at that memory location cannot be modified.
4
#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
#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
#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
#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
Pointed_type *Array_name[Size] int *a[20]; void display_array(int *q[]) { /*...*/ }
9
10
value indirectly pointed to by a pointer to a pointer, you must apply the asterisk operator twice.
#include <stdio.h> int main(void) { int x, *p, **q; x = 10; p = &x; q = &p; printf("%d", **q); /* print the value of x */ return 0; }
11
valid value, you will probably crash your program
valid memory location is given the value null.
nothing and should not be used.
12
char *p = 0; p = NULL;
int *p = 0; *p = 10; /* wrong! */
13
that can be assigned to a pointer.
and it is the address used when the function is called.
function can be called through that pointer.
passed as arguments to other functions.
14
#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
#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
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
spaces during execution.
library function malloc().
satisfy the malloc( ) request, an allocation failure
in that it returns previously allocated memory to the system.
18
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