You can point to anything, including other pointers.
#include <stdio.h> #include <ctype.h> void find( char *str, char **first_vowel, char **last_vowel ) { char *p = str; *first_vowel = NULL; *last_vowel = NULL; while ( *p ) { char ch = tolower(*p); if ( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) { if ( *first_vowel == NULL ) { *first_vowel = p; } *last_vowel = p; } p++; } } int main( void ) { char *str = "Now is the time for lunch."; char *first_vowel; char *last_vowel; find( str, &first_vowel, &last_vowel ); printf( "First vowel = %c at position %d\n", *first_vowel, first_vowel - str ); printf( "Last vowel = %c at position %d\n", *last_vowel, last_vowel - str ); return 0; }
First vowel = o at position 1 Last vowel = u at position 21
Note that this code is dangerous because we are not checking for
NULL
. Both first_vowel
and last_vowel
might come back from
find()
with the value of NULL
. In real code, we would check
for this before passing them to printf()
or performing pointer
arithmetic.