A pointer with every word from a sentece

Hi there, I was trying to make a chat bot, that learns from human and generate a sentece, etc, etc, so the first step is to transform a sentece in a dynamic array of words, and learn them. But I meet a problem
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

using namespace std;


char** sscanf(char* hstring)
{
	int count = 0;
	char* current = hstring;
	while (*current)
	{
		if (*current == ' ')
		{
			count++;
		}
		while (*current == ' ')
		{
			current++;
		}
		if (*current)
			break;
		current++;
	}
	char** result = new char*[count];
	current = hstring;
	char* nstr = new char;
	char* rem = new char;
	int c = 0, i = 0;
	while (*current)
	{
		if (*current == ' ')
		{
			*(++result) = nstr;
			
			nstr = nullptr;
			nstr = new char;
		}
		while (*current && *current == ' ')
		{
			current++;
		}
		while (*current && *current != ' ')
		{
			if (!*current) break;
			*(nstr++) = *current;
			current++;
		}
	}


	return result;
}


int main()
{
	char** prop = sscanf("Sall, ce faci?");
	cout << prop[1];
	cin.get();
}

this code in visual studio redirects me to
1
2
3
4
	static size_t __CLRCALL_OR_CDECL length(_In_z_ const _Elem * const _First) _NOEXCEPT // strengthened
		{	// find length of null-terminated string
		return (_CSTD strlen(_First));
		}

with error
 
Unhandled exception at 0x0F3F3E90 (ucrtbased.dll) in Chat Bot.exe: 0xC0000005: Access violation reading location 0x0B00AD04. occurred
1
2
3
4
5
6
7
		while (*current && *current == ' ')
		{
			current++;
		}
		// suppose current points to (a string that ends with) " ", where does current end up pointing to?
		// if (*current) checks if the value of current is not zero (if it points to somewhere in memory). 
		// it does not check if current is pointing at your char array or even memory assigned to your program. 


I would like to recommend you to stop using char pointers (or pointers to char pointers) and start using std::string as soon as possible. You will probably find that std::string has some more/safer features that will probably make your life easier.

Kind regards, Nico

Note that char pointers can also be very useful, if used with the right precautions (at least checking for '\0'). But usually if you can use a std::string: try those first.
Last edited on
I used pointers because I want to became familiar with them. I heard they are a powerful tool. So I would like to know how I can fix my issue.

This code initialy was with string but I felt that I need to be familiar with pointers
A char array should always have a known length or (preferably) end with a null-termination ('\0'). If you don't use std::string you will have to make sure that this termination is indeed present.

If your char array is null-terminated or if you know the length of your char array you should make sure that you never iterate past the end of the char array (the easiest way is to check if the pointer currently points to '\0' before you iterate further, but you can also compare with the known length).

In any case it is your responsibility to make sure that your pointer points to somewhere inside your char array. You don't check for this, so your pointer will iterate out of the assigned memory and you end up with an access violation.
So I edit a little bit code
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
40
41
42
43
44
45
46
47
char** sscanf(char* hstring)
{
	int count = 0;
	char* current = hstring;
	while (*current)
	{
		if (*current == ' ')
		{
			count++;
		}
		while (*current == ' ')
		{
			current++;
		}
		if (*current)
			break;
		current++;
	}
	char** result = new char*[count];
	current = hstring;
	char* nstr = new char;
	int c = 0, i = 0;
	while (*current)
	{
		if (*current == ' ')
		{
			*(++result) = nstr;
			
			nstr = nullptr;
			nstr = new char;
		}
		while (*current != '/0' && *current == ' ')
		{
			current++;
		}
		while (*current != '/0' && *current != ' ')
		{
			if (!*current) break;
			*(++nstr) = *current;
			current++;
		}
		*nstr = '/0';
	}


	return result;
}

where's the issue?
the first step is to transform a sentece in a dynamic array of words

so, it appears, that your basic objective is to split a string into its component words. There are several ways to do this: http://stackoverflow.com/questions/236129/split-a-string-in-c
also remember to remove any punctuation from the back of the individual words
I just want to show me, where in my code is a mistake, and how I can correct it
Topic archived. No new replies allowed.