char handeling...

So I was working on some kind function that can make a sentente into lot a word array. It compile fine, but I get some errors with it;
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
#include <iostream>

using namespace std;

char** sprwords(char* hstring)
{
	char** output = new char*;

	for (; *hstring;)
	{
		output += *hstring;
		*hstring++;
		if (*hstring)
		{
			*hstring++;
			*output++;
		}
	}
	return output;
}

int main()
{
	char** test = sprwords("test1 test2 test3");
	cout << *(test + 1);
}

so when I run it visual studio gives me an error
"Exception thrown: read access violation.

_First was nullptr.

If there is a handler for this exception, the program may be safely continued."

after this error it moves me there
1
2
3
4
5
	static size_t __CLRCALL_OR_CDECL length(const _Elem *_First)
		{	// find length of null-terminated string
		return (*_First == 0 ? 0
			: _CSTD strlen(_First));
		}

visual studio signalise this line: return (*_First == 0 ? 0
you do not appear to have allocated any memory for the internal arrays of char.
when you make a char**, you have to new twice, once on the outside for the array of strings, and again for each string for its max length. This is typically done in a loop..

char ** foo;
foo = new char *[10]; //an array of 10 unalloacated char pointers.

for(i = 0; i<10; i++)
foo[i] = new char[100]; //now each char array in your array of arrays has memory also.

delete in reverse
there is a problem, I don't know how big or small will it be(pointer)...
If you don't know how big your array needs to be then don't use one.
Better to use a vector or linked list
My code presume:
1 – you are not trying to write your own version of strtok();
2 – you can’t, for any reason, use c++ containers (otherwise the solution would be less then trivial);
3 – you can ensure that your strings are null-terminate.

Hope this can be of some inspiration:
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
#include <cstring>
#include <iostream>

struct ArrayAndBoundary{
    char** astring;
    int dimension;
};

ArrayAndBoundary sprwords(char* hstring);

int main()
{
    ArrayAndBoundary myarray = sprwords("test1 test2 test3");
    for(int i{0}; i<myarray.dimension; i++)
        std::cout << "arrabou.astring[" << i << "] = "
                  << myarray.astring[i] << std::endl;

    free(myarray.astring);

    return 0;
}

ArrayAndBoundary sprwords(char* hstring)
{
    int hstringlen = strlen(hstring);
    // Get a read-write memory area
    char* entirestring = (char*)malloc(sizeof(char) * (hstringlen+1));
    // populate that area
    entirestring = strcpy(entirestring, hstring);

    int spaces{0};
    // Analize entirestring for spaces
    for(int i{0}; i<hstringlen; i++)
        if(' ' == entirestring[i])
            spaces++;

    ArrayAndBoundary arrabou;

    // The number of word will be number of spaces + 1;
    arrabou.dimension = spaces + 1;
    arrabou.astring = (char**)malloc(sizeof(char)*(arrabou.dimension));

    // Initialize strtok();
    char* word = strtok(entirestring, " ");
    // Loop inside entirestring:
    for(int i{0}; NULL != word; i++) {
        arrabou.astring[i] = (char *)malloc(sizeof(char)
                                                * (strlen(word+1)));
        arrabou.astring[i]= strcpy(arrabou.astring[i], word);
        word = strtok(NULL, " ");
    }

    // Don't forget to release unused memory
    free(entirestring);
    free(word);

    return arrabou;
}

Topic archived. No new replies allowed.