Sunday, July 12, 2009

A program in C++ pointers.?

please help me write a program to search for a given character and print the streing from the point of match using pointers.





i know to do it without pointers but i am asked by my sir to do it using pointers. please help me. you wouldn't be doing my home work but really helping me......


please help

A program in C++ pointers.?
First things first, 'char * s' is the same as the character array 'char s[].' In this example, s is what is commonly called a c-style string, which is a null-terminated array of characters.





For example, if you use the string 'cat', the c-string would be the character array {'c','a','t',0 (the number zero, not the character zero)}.





Just to note, c-strings can be initialized easily like this:


char s[] = "cat";





As I said before, char * s is the same as char s[] and they are interchangeable. In both cases, s points to the address of the first element of the array. When you say s[index], you are getting the value of the object at memory location s + index. You can do the same thing with pointers by first adding index to s and then dereferencing s, like so: *(s+index). This is what is going on in this example.





Lets say we're using the string 'cat' and the character we're looking for is 'a'. So this algorithm starts off with 'cat' being stored in a null-terminated character array s.





%26gt;while((c!=*s) %26amp;%26amp; (*s)){





This will loop as long as the character at s in not equal to character c and while the character at s in not equal to zero. For our first iteration, s is still pointing to the first character of the string and by dereferencing s (via *s) we get the character 'c'. Since this is neither equal to the character stored in c, 'a', nor is it equal to zero, we will continue.





When the end of the string is reached and s points to the null-terminating character, zero, the while loop will end because in C a value of zero is the same as false.





%26gt;s++; }





This increments the pointer, essentially behaving as an iterator. When you increment a pointer, it then points to the next character in the array, which in our case would be 'a'. Also note that the string represented by c-strings go from the character the pointer points to the null-character, so doing this effectively removes the first character and makes the string 'at'.





After s is incremented, the next iteration will exit the while loop because now *s = 'a'





%26gt;return s;





this obviously returns s, which now points to the c-string 'at'.





To hide the confusing parts of pointers, you could also do it like this:





int index = 0;


while( s[index] != c %26amp;%26amp; s[index] ) {


index++;


}


return s;





This method still uses pointers, except they are referenced in a more easily understood way.





Pointers are a very important topic. They may seem difficult or confusing at first, but once you get the hang of them they can be a very powerful and useful tool that will make your life much much easier.
Reply:const char * strchr(const char *s, char c)


{


char c2;





for (const char *p = s; (c2 = *p) != 0; ++p)


if (c2 == c) return p;





return null;


}


No comments:

Post a Comment