开发者

Putting tokens in a multi dimensional array?

开发者 https://www.devze.com 2023-04-05 07:02 出处:网络
How do you tokenize a string and put it in a mulitdimensional array, letter by letter? Im getting the following error \"invalid conversion from char*\' tochar\".

How do you tokenize a string and put it in a mulitdimensional array, letter by letter? Im getting the following error "invalid conversion from char*' tochar".

    void tokens( char *sptr)
{
    int i;
    char *p, tokens[100][16];

    while (p != NULL)
    {
        for ( i = 0; i <= 100; i++)
        {
            for (int j = 0; j <= 16; j++)
            {
                p = strtok (sptr, " ,.-"开发者_Python百科);

                tokens[i][j] = p;

            }
        }
    }
}


I'm not sure if you really want to copy each character manually here.

But you could do something like this:

int i, j;

for(i = 0; i < 100; i++)
{
    p = strtok(sptr, " ,.-");
    if(p == NULL) break;
    for (j = 0; j < 16; j++)
    {
        tokens[i][j] = p[j];
        if(*p++ == 0) break;
    }
    tokens[i][j] = 0; /* add ending \0 */
}

or simpler:

int i, j;

for(i = 0; i < 100; i++)
{
    p = strtok(sptr, " ,.-");
    if(p == NULL) break;
    strcpy(tokens[i], p); /* strncpy would be better */
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号