Pointers are values. If you have a pointer pointing to the start of an array (s) and you have a pointer pointing further along in that same array (p), then the difference between (p) and (s) is how far along the array you are.Code:int mystrlen( char *s ) { char *p = s; /* make p point to s */ while( p && *p ) /* while p is not null, and *p is not \0 */ p++; /* increment p */ return p - s; /* return the difference between p and s */ }So, if the loop fails right off, then p and s will be pointing to the same. So, p - s will be zero. If we advance P one spot, then p - s will be 1. And so on.Code:p = s; p++; if( p - s == 1 ) printf( "p is 1 element along the array" );
As for the recursive function:So, if we have a string that is 2 characters long, not counting the nul:Code:check to see if s is NULL, and not '\0' if it's not return 1 (counting that it's a valid non-nul character) + whatever further recursive calls returnSo, 0 + 1 + 1 = 2.Code:if s and *s return 1 (the first character) + (recursive call, points to the next character) if s and * s return 1 + recursive call now will be \0 if s and *s ..fails because it's now \0, so it returns 0
Quzah.
Hope is the first step on the road to disappointment.
ok well i understood the first part but the recursive (above) part is just playing with my mind a little but im slowly making sense of itAs for the recursive function:So, if we have a string that is 2 characters long, not counting the nul:Code:check to see if s is NULL, and not '\0' if it's not return 1 (counting that it's a valid non-nul character) + whatever further recursive calls returnSo, 0 + 1 + 1 = 2.Code:if s and *s return 1 (the first character) + (recursive call, points to the next character) if s and * s return 1 + recursive call now will be \0 if s and *s ..fails because it's now \0, so it returns 0
Quzah.
so far ive understood that whilst the end of the string (\0) is not achieved then you return 1 and something else. what i need to know is what is the difference between 'NULL' and '\0'?
and
how do you put the recursive function into code (what fills the (...)'s after return 1?
your such a help Quzah thanks loads
I was just illustrating that it was recursing there. That wouldn't actually be in the function itself. Here is the difference between NULL and a nul character:The pointer p is set to "point nowhere". The character c contains a nul character, meaning the value is zero. If we do this:Code:char *p = NULL; char c = '\0';Then the pointer p is actually pointing some place (it's pointing at the character c, whose value is nul). So, if we did:Code:p = &c;Edit: The nul character (or terminator) is generally used to indicate the end of strings.Code:if( p == NULL ) printf( "this won\'t print because the pointer actually points some place" ); else if( *p == '\0' ) printf( "this will print because the value of c is nul" ); else printf( "this won\'t print because the value of c is nul" );
Quzah.
Last edited by quzah; 03-24-2010 at 04:51 PM.
Hope is the first step on the road to disappointment.
ow ok cool thanks you cleared a few dark clouds now
thanks time to work on this tomorrow now
@Quzah.
Thank you so much for your through explanation, I did learnt a lots from it.