const char * to vector array char * error

Hello,

I'm trying to do a quick conversion from a const char * called to a function to a vector array for comparison. I'm doing a quick aka dirty conversion so nothing special.

This is the test code which might have a simple solution.

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
#include <vector>
#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;

void loadScene(const int mode, const char * str);

int main(void)
{
    loadScene(1,"test apple");

    return 1;

}

// code to handle actual commans
void loadScene(const int mode,  const char * str)
{

    /// Create array
    std::vector<char *> arguments;


    /// Reserve Space for 32 arguments
    arguments.reserve(32);

    // create token
    char * token_pointer;
    int unsigned idx=0;

    /// find first pointer
    token_pointer = strtok ((char *)str," ");

    /// loop through string
    while(token_pointer!= NULL)
    {

        token_pointer = strtok ((char *)str," ");
        idx++;
    }

    //
    cout << "argument 0" << arguments[0];
    cout << "argument 1" << arguments[1];

    return;
}


Any ideas?

Vivienne
Casting the const away in loadScene results in undefined behavior. You cannot modify a string literal. You could also benefit from looking up the function you're attempting to use.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// http://ideone.com/NgLZ9i
#include <iostream>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>

std::vector<std::string> tokenize(const std::string& s)
{
    using iter = std::istream_iterator<std::string>;

    std::istringstream is(s);
    return std::vector<std::string>(iter(is), iter());
}

int main()
{
    auto tokens = tokenize("test apple");

    for (auto& token : tokens)
        std::cout << '"' << token << "\"\n";
}
I will check the function again.

I'm not too familiar with the code "auto". Usually I don't mess with "auto" which I think is a pain to work with especially reading someone else code. Since you have to figure out what "auto" is being used for.

1
2
3
    std::vector<char *> arguments;
    /// Reserve Space for 32 arguments
    arguments.reserve(32);

The comment is misleading. You preallocate memory for storing 32 pointers.

strtok modifies the input string. It writes null characters to the parameter.

Only the first call to strtok takes a string; the other call must use NULL.
Ah. I have to look. I noticed some code that leaves me from messing with the whole strtok thing. Here it is. The function seems to work.

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
// code to handle actual commans
void loadScene(const int mode,  const char * str)
{

    /// string string leaving something comparable
    string argumentsstring = str;
    string argument[10];

    /// create a idx
    int idx = 0;

    /// copy string to stream
    stringstream ssin(argumentsstring);

    /// loop through arguments
    while (ssin.good() && idx < 10){
        ssin >> argument[idx];
        ++idx;
    }


    cout << argument[9];

    return;
}
Topic archived. No new replies allowed.