Character pointers

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;
}
Why do you think the function is wrong?
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.


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.
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
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' ;}



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*
I'm not sure why you think there's a problem. There is no 'a' used anywhere a char* is expected.
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.
You want a location... You get a memory pointer...
Add a counter and return the value of the counter...
The big difference between your function and std::strchr is that std::strchr returns a null pointer if the character is not found.
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.