Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

load tokens into a multi dimensial array

Name: Anonymous 2011-09-17 4:07

How do you load tokens into  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, " ,.-");

                tokens[i][j] = p;

            }
        }
    }
}

Name: Anonymous 2011-09-17 4:17

lol, terrible.

Name: n3n7i 2011-09-17 4:43

http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html

tokens[i][j] = p; << error'ed line

cast to char ?

tokens[i][j] = (char)p;

Name: n3n7i 2011-09-17 4:46

p might return more than one char though..

Name: n3n7i 2011-09-17 5:04


void tokens( char *sptr){

  int i=0, j=0, k=0;
  char *p, tokens[100][16];

  p = strtok (sptr, " ,.-");

  while (p != NULL){   
   
    k = strlen(p);   
   
    for (int j = 0; j <= k; j++){
        tokens[i][j] = p[j];
        }
    i++;
    p = strtok (sptr, " ,.-");
    if(i==100) break;
    }
}

Name: n3n7i 2011-09-17 8:16

Compiled version...

#include <string.h>

void toke(char *sptr){

  int i=0, j=0, k=0;
  char *p, tokens[100][16];

  p = strtok (sptr, " ,.-");
  while (p != NULL){
   
    k = strlen(p);   
    for (j = 0; j <= k; j++){
        tokens[i][j] = p[j];
        }

    printf("%s\n", &tokens[i][0]);

    i++;
    p = strtok ([b]NULL[/b], " ,.-");
    if(i==100) break;
    }
}

 
int main(int argc, char *argv[]){ 
  toke(argv[1]);
  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List