returning a pointer

Have one question and one issue:
The question- I have to write a function of type pointer to char that returns a pointer when it finds a match.

(This is the actual exercise question):

Write a function that returns a pointer to the first occurrence of the character c in the string s, or nullptr if there is no match.
char* find(char s[], char c) (Horstmann, 2017-08-12,


and this is the code i wrote for it so far:

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
#include <iostream>
#include <cstring>
using namespace std;


char* find(char s[], char c)
{
	int size = strlen(s);
	char* p = nullptr;
	for (int i = 0; i < size; i++)
	{
		if (s[i] == c)
		{
			return p = s + i;
		}
	}
	return p;
}

int main()
{
	 char word[] = "James";
	cout << find(word, 'm');
	return 0;
}


So is this what they are asking for? I thought by "returning a pointer" they were asking for the address of the character that is matched. But it seems that it returns the characters starting with that match. Is this because its a type char so thus giving the address as type char?

The issue i have is with the nullptr. when i return it i get an error. I'm assuming that it has to do with the return type being a pointer to type char?
It correctly returns your pointer, containing the address.

The stream insertion operator << has an overload that regards this as a c-string, so it prints out the remaining letters.

The operator << doesn't know what to do with a nullptr.


You can try
1
2
3
4
5
6
7
8
9
10
11
#include <cstdio>

// .....

int main()
{
	char word[] = "James";
	char *p = find( word, 'm' );
	if ( p ) printf( "Address: %p, value: %c\n", p, *p );
	else     printf( "Character not found\n" );
}

Address: 0x7b605ce053f2, value: m



Alternatively you could cast to a different type of pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using std::cout;

char* find( char *s, char c)
{
   for ( ; *s; s++ ) if ( *s == c ) return s;
   return nullptr;
}

int main()
{
   char word[] = "James";
   char *p = find( word, 'm' );
   if ( p ) cout << "Address: " << (void *) p << "   value: " << *p << '\n';
   else     cout << "Letter not found\n";
}
Last edited on
There are several C functions that return a nullptr. The problem you have struck is how you use the result.

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

char* find(char* s, char c)
{
    while( *s != '\0')
    {
        if(*s == c)
            return s;
        
        s++;
    }
    
    return nullptr;
}

int main()
{
    char word[] = "James Paddlewheel";
    
    if(find(word, 't') == nullptr)
        std::cout << "Not found\n";
    else
        std::cout << "Found\n";
    
    return 0;
}


Not found
Program ended with exit code: 0
Or, a (minor) alternative:

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
#include <iostream>
//#include <cstring>

char* find(char* s, char c)
{
    while( *s != '\0')
    {
        if(*s == c)
            return s;
        s++;
    }
    return nullptr;
}

int main()
{
    char word[]{"James Paddlewheely"};
    char search_character{'y'};
    
    char *ch = find(word, search_character);
    
    if(ch == nullptr)
        std::cout << '\'' << search_character << "' not found\n";
    else
        std::cout << '\'' << *ch << "' found\n";
    
    return 0;
}


'y' found
Program ended with exit code: 0
Last edited on
specifically for c-strings you could also return the pointer to the 0 end of string marker character.
then its just
if(*ch) //the POINTER is valid, the CHARACTER POINTED TO is 'outside' the string and special
//found
else //not found
and avoid the entire aggravation with nullptrs.
to do this, in the above find() code, return s instead of nullptr. the loop condition already ensures that s was the end of string marker... so its ready to go.
Last edited on
so its ready to go.

Unfortunately Ms Horstmann required a nullptr :)
Topic archived. No new replies allowed.