Home › Forums › C Programming › Understanding Pointers and Strings › Re: Re: Understanding Pointers and Strings
March 26, 2009 at 7:06 pm
#3538
GWILouisaxwzkla
Participant
Heres how I might do this ( this uses pointer arthimatic instead of indexes ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <br /> <br /> #include <stdio.h><br /> #include <iostream.h><br /> <br /> void draw(char *w, int len);<br /> <br /> int main()<br /> {<br /> <br /> char words [ 100 ] = ("This is my hundredth attept st this extremely annoying task :)");<br /> char * index = NULL;<br /> int len = 21;<br /> <br /> draw( words + 11, len );<br /> <br /> <br /> <br /> return 0;<br /> }<br /> <br /> void draw ( char * words, int lengthOutput ) //words points to words [ 11 ]<br /> <br /> {<br /> <br /> char * upperLimit; //pointer for storing upper limit of output<br /> <br /> <br /> upperLimit = words + lengthOutput; //upperLimit points to the 32nd char of words [ 100 ]<br /> putchar ( 'n' ); //output newline<br /> while ( words != upperLimit ) //while not at the 32nd char of array<br /> {<br /> putchar ( * words); //output the current char<br /> words ++; //goto next char in the array<br /> }<br /> putchar ( 'n' ); //output a newline<br /> <br /> }<br /> <br /> <br /> |