Home › Forums › C Programming › Understanding Pointers and Strings
- This topic has 4 replies, 2 voices, and was last updated 15 years, 8 months ago by JonathaThurston.
- AuthorPosts
- March 26, 2009 at 3:52 am #2183JonathaThurstonParticipant
Hey Guys,
I have a question regarding pointer arithmetic as I find it very hard to grasp it. Well to be totally honest I do not understand the concept at all so I wrote this program which sends a char array to a function and prints out a string of characters from a certain offset to a certain length.
I am having trouble with the loop in the function can you please help me understand it.
Thanks.
123456789101112131415161718192021#include<stdio.h><br /><br />void draw(char *w, int len);<br /><br />int main(){<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 />return 0;<br />}<br /><br />void draw(char *w, int len){<br />char *index = NULL;<br />index = w;<br /><br />for(index; *index != *(index + len); index++)<br />putchar(*index);<br />} - March 26, 2009 at 5:11 pm #3536GWILouisaxwzklaParticipant
To understand the problem completely, remember that arrays are configured as contiguous blocks of memory ( the first index in memory precedes the next and so on ) and realize the fact that pointers are simply variables that hold the address of other variables ( the size depends on your hardware and operating system ). Also keep in mind that in C the semantic value of an array’s name is the address of the first item in the array ( array [ 0 ] ). So that in your program you have a situation like :
1234<br /><br />draw(words + 11, len); //pass in the address of the 11th 8-bit item in memory, words [11]<br />since the array looks like this in memory ( let 0x0065fd94 be the first address of the array ):
memory address item
0x0065fd94 ‘T’
0x0065fd95 ‘h’
0x0065fd96 ‘i’
0x0065fd97 ‘s’
…………..and so on your loop:
123456<br /><br />for(index; *index != *(index + len); index++)<br />putchar(*index);<br />}<br />starts at index == words [ 11 ] which is ‘h’ and continues until ( *index == *(index + len); ) . But since * ( index + len ) is words [ 32 ] == ‘h’ your loop never executes. I think what you wanted to do is this:
1234567891011121314151617181920212223242526272829303132333435363738394041<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 /><br /><br />draw( words + 11, len );<br /><br /><br /><br />return 0;<br />}<br /><br />void draw ( char * w, int len )<br /><br />{<br />char * index = NULL;<br />int i = 0;<br /><br />index = w;<br /><br />putchar ( 'n' );<br />for( ; i < len ; index ++ , i ++ )<br />{<br />putchar ( * index );<br />}<br />putchar ( 'n' );<br />}<br /><br /><br /><br /> - March 26, 2009 at 6:59 pm #3537JonathaThurstonParticipant
Hey dman thanks for explaining it makes more sense now. Is it possible to avoid making *index a pointer to the array and do the manipulations on *w instead?
- March 26, 2009 at 7:06 pm #3538GWILouisaxwzklaParticipant
Heres how I might do this ( this uses pointer arthimatic instead of indexes ) :
1234567891011121314151617181920212223242526272829303132333435363738394041<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 /> - March 27, 2009 at 1:17 am #3539JonathaThurstonParticipant
Yea that makes more sense actually, and its more readable than a for loop.
Thanks
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.