Character pointers

Feb 13, 2012 at 2:49am
Writing the strchr function using pointers. Can anyone tell me what I'm doing wrong here?

char* pStrChr( char* str, int character )
{
while( *str )
{
if ( *str == character )
{
break;
}
*str++;
}
return str;
}
Feb 13, 2012 at 2:55am
Why do you think the function is wrong?
Feb 13, 2012 at 2:59am
Because after I compile, instead of telling me where the specific character is located, it gives me the string starting with that character instead. I thought that it should return *str instead. However, it gives me an error doing that.
Feb 13, 2012 at 3:04am


You really make your code so much harder to read when you don't indent at all. Here is an example of how you could format that so it's readable.

1
2
3
4
5
6
7
8
9
10
11
12
char* pStrChr( char* str, int character )
{
    while( *str )
    {
        if ( *str == character )
        break;
    
        *str++;
    }
    
    return str;
}


Find more information by just using google to find a solution for strchr.
Feb 13, 2012 at 3:08am
Sorry. When I copy and pasted, it took the indents out. Didn't realize I needed to click the source code thing to keep indents.

1
2
3
4
5
6
7
8
9
10
11
12
char* pStrChr( char* str, int character )
{
	while( *str )
	{
		if ( *str == character )
		{
			break;
		}
		*str++;
	}
	return str;
}


I googled half of today trying to figure out this solution.
Last edited on Feb 13, 2012 at 3:12am
Feb 13, 2012 at 6:34am
There is nothing wrong with your code.

Well,

*str++;

should be

str++;

But that doesn't have any impact on what the code actually does. Which is find a character that matches and returns a pointer to it.

I suspect the problem is how you're dealing with the return value.

For instance:

1
2
char* s = "xbacxefx" ;
char * a = pStrChr(s, 'a') ;



a is a pointer and holds the memory location where 'a' first appears in s. You seem to be expecting it to return the character found. Why would it do that? You already know what that character is. What you need to know is where that character is, and that is exactly what you get.

Maybe an illustration would be better. The following finds and replaces 'x' in the string pointed to by s using your implementation of pStrChr.

1
2
3
4
5
6
7
8
9
10
11
12
	char s[] = "xbacxefx";

	std::cout << s << '\n' ;

	char* x = pStrChr(s, 'x');
	while (*x)
	{
		*x= 'o' ;
		x = pStrChr(x, 'x') ;
	}
	
	std::cout << s << '\n' ;}



Feb 14, 2012 at 5:49pm
I think the problem is is that it has to be char * strchr ( char * str, int character );
character, or 'a' as you put it, is an int, not a char*
Feb 14, 2012 at 7:59pm
I'm not sure why you think there's a problem. There is no 'a' used anywhere a char* is expected.
Feb 14, 2012 at 10:52pm
When I put
1
2
3
4
5
6
7
8
9
10
11
12
char* pStrChr( char* str, int character )
{
	while( *str )
	{
		if ( *str == character )
		{
			break;
		}
		*str++;
	}
	return str;
}


through the compiler, it returns the balance of the string, not the position of the character. For example, if str = "abcd" and character = 'c' it returns "cd" not 3 like it's supposed to.
Feb 15, 2012 at 12:08am
You want a location... You get a memory pointer...
Add a counter and return the value of the counter...
Feb 15, 2012 at 1:20am
The big difference between your function and std::strchr is that std::strchr returns a null pointer if the character is not found.
Feb 15, 2012 at 2:59am
through the compiler, it returns the balance of the string, not the position of the character. For example, if str = "abcd" and character = 'c' it returns "cd" not 3 like it's supposed to.


No, it returns the address of the character found. The address of the character 'c' in the quote is the same as the address of "cd".

Does the following help any?


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
#include <iostream>
#include <cstddef>

char* pStrChr( char* str, int character )
{
	while( *str )
	{
		if ( *str == character )
		{
			break;
		}
		str++;
	}
	return str;
}

int main()
{
	char s[] = "this is some text.";

	std::cout << "s as a c string: " << s << '\n' ;
	std::cout << "It's address is " << int(&s[0]) << '\n' ;

	char * tPos = pStrChr(s, 't') ;
	while ( *tPos )
	{
		std::cout << "found a 't' !\n" ;

		std::cout << "The location of the t is " << (int)tPos << '\n' ;
		std::cout << "Derefencing the pointer = " << *tPos << '\n' ;

		std::ptrdiff_t dist = tPos - &s[0] ;
		std::cout << "Distance from s = " << dist << '\n' ;
		// notice any relation between dist and the index into s of
		// the character we found?

		tPos = pStrChr(tPos+1, 't') ;
	}
}

Topic archived. No new replies allowed.