Returning A Partial Character String From A Funcion

I'm trying to develop a function for a larger exercise, which is to receive a character string, and return a substring. The function I came up with essentially works, but it has some flows. I passed the string "one two three" to the function, and it is supposed to return the substring "one". The function does to this, however, it also returns a couple of weird characters alone with "one". The output is "one**". (It doesn't actually return asterisks. I just used those as placeholders.) I figured out that the extra symbols are caused by the other two substrings.

What I don't quite understand is, in line 13, I created a for loop that copies the string to another variable. The loop is set up to only copy the first substring, terminating at the first space in the original string. Obviously, the copied variable has received addresses for the other substrings as well, and the extra output is caused by "junk" address. What I haven't quite figured out is why the copied string expands farther that the first substring, since that's all I'm copying from the original string.

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
1 #include <iostream>
2 #include <cstring>
3
4 using std::cout ;
5 using std::endl ;
6
7 char* passFunction( char* pass )
8 {
9 char* search = " " ;
10 char* position = strstr( pass, search ) ;
11 char* output = new char ;
12
13 for ( int i = 0 ; i < position-pass ; i++ )
14 {
15 *( output + i ) = *( pass + i ) ;
16 }
17
18 return output ;
19 }
20
21 int main( void )
22 {
23 char* sentence = "one two three" ;
24
25 cout << passFunction( sentence ) << endl ;
26
27 sentence = nullptr ;
28
29 return 0 ;
30 }
Part of the problem is that output is a pointer to a single character, not a string. Also you shouldn't be assigning sentence to a nullptr, sentence should also be a const char*.

Is there a reason you're trying to use C-strings instead of std::string?
Line 10: The position could become null, if there is no search in pass. You don't check for that explicitly (although the loop condition hopefully fails).

Line 11: You need a character array long enough to hold the found word (+1 for terminating null).

Line 25: The function returns the address of a dynamically allocated memory block, but you don't store it. Memory leak.

Line 23: The address of a string literal is a const char *. Any attempt to modify a string literal is undefined behaviour. Your compiler should at least mention that fact.

Line 27: is not a real error; it just makes it impossible to refer to the string literal later.
Part of the problem is that output is a pointer to a single character, not a string. Also you shouldn't be assigning sentence to a nullptr, sentence should also be a const char*.

Is there a reason you're trying to use C-strings instead of std::string?


Yes. This is part of a larger exercise from a book that I'm studying. I'm trying to use only material covered in the book, so I can compare my solution to the one in the book.
What book are you using? Maybe you should find a better book, one that teaches C++ rather than C with classes.

"Beginning Visual C++ 2010" by Ivor Horton. In this section, it said that it deals with string class in chapter 8, and right now I'm only on chapter 5.
Topic archived. No new replies allowed.