/* * ===================================================================== * ptrcls2.c -- first of a series of examples to illustrate pointers and * arrays at work and their relationships. * * Copyright (C) 1998 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written by : David Mazzoni * ===================================================================== */ #include /* for printf() prototype */ #include /* for strlen() */ int main( void ) { static char caArray[] = "character array"; char * cpChar = caArray; /* pointer to character array */ int iCnt; /* counter for indexing */ /* [a] : Show some things like size of each, values, etc. */ printf( "The value of \"caArray\" is %x\n", caArray ); printf( "The value of \"cpChar\" is %x\n", cpChar ); printf( "The size of \"caArray\" is %d\n", sizeof( caArray ) ); printf( "The size of \"cpChar\" is %x\n", sizeof( cpChar ) ); printf( "The address(&) of \"caArray\" is %x\n", &caArray ); printf( "The address(&) of \"cpChar\" is %x\n", &cpChar ); /* [b] : Manipulate character array */ printf("\n"); for( iCnt = 0; iCnt < strlen(caArray); ++iCnt ) { printf( "\ncaArray[%-2d] = %c\tcpChar[%-2d] = %c %-2d[cpChar] = %c" "\t *(cpChar + %-2d) = %c", iCnt, caArray[iCnt], iCnt, cpChar[iCnt], iCnt, iCnt[cpChar], iCnt, *(cpChar+iCnt) ); } /* [c] : Now use the pointer as the variable */ printf("\n\nUsing pointer incrementing for caArray = "); for( iCnt = 0; iCnt < strlen( caArray ); ++iCnt ) putchar( *cpChar++ ); puts(" "); }