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*' to
char".
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 */
}
精彩评论