/* john pfeiffer 2010-06 explaining to myself string character pointer arithmetic */
/*
buffer[32] = HELLO WORLD
buffer[0] = 2293536
buffer[0] = first char is H
charptr = 2293536
*charptr = first char is H
sizeof a char 1
*(charptr+1) = second char is E
charptr = charptr + sizeof( c );
*(charptr+1) = third char is L
keywordlocation = strstr( buffer , 'WORLD' );
keywordlocation = 2293542
*keywordlocation = W
WORLD
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int i =0;
char c;
char buffer[32];
char* charptr = NULL;
char* keywordlocation = NULL;
strcpy( buffer , "HELLO WORLD" );
printf("buffer[32] = %s\n" , buffer );
charptr = buffer;
printf("buffer[0] = %d\n" , buffer );
printf("buffer[0] = first char is %c\n" , buffer[0] );
printf("charptr = %d\n" , charptr );
/* dereference the pointer to see the letter stored at that address*/
printf("*charptr = first char is %c\n" , *charptr );
printf("\nsizeof a char %d\n" , sizeof( c ) );
printf(" *(charptr+1) = second char is %c\n" , *(charptr+1) );
printf("charptr = charptr + sizeof( c );\n" );
charptr = charptr + sizeof( c );
printf(" *(charptr+1) = third char is %c\n" , *(charptr+1) );
printf("\nkeywordlocation = strstr( buffer , 'WORLD' ); \n" );
keywordlocation = strstr( buffer , "WORLD" );
printf("keywordlocation = %d\n" , keywordlocation );
printf("*keywordlocation = %c\n" , *keywordlocation );
/* print the length of keyword chars by char pointer arithmetic */
for( i = 0 ; i < strlen( "WORLD" ) ; i++ )
{
printf("%c" , *keywordlocation );
keywordlocation = keywordlocation + sizeof( c ) ;
}
return 0;
}/*end of main*/