for example...if i hav to search for a "//" in a buff...
{
//asnbvfv hjegfub hgdyfgh cjs
}
and if my pointer is pointing to cjs.... how can i get back to //...???
thanx in advance
How can i go backwards in a character buffer in 'C'?
Decrease the value of the pointer (for example, by using the -- operator) and it will go to the left. Increase it and it will go to the right.
Reply:decrement your pointer-- and it will move back one char,
then check the dereferenced char value of where it's pointing now and the place it was pointing
do{
pointer--;
}
until ("/" == pointer[0] %26amp;%26amp; "/" == pointer[1] )
something like that, don't lock it up in some infinite loop...
Reply:Yes, you can decrement and increment the pointer as much as you want, provided the buffer still contains the characters, i.e. if they haven't been popped off a stack or something similar. In the case you're describing, you can simply set the pointer offset to zero to access the first character.
But perhaps you mean you're getting characters one by one from the keyboard or a file and want to go back and look at previous characters? In that case, as they come in send the characters to your own buffer which you can examine at will.
Bear in mind that pointer arithmetic is always a minefield and indexing something outside a buffer's valid range is a very common error.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment