Thread: tokenize a string

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    12

    tokenize a string

    help me out.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    #define DELIM   " , \n" 
    #define MAXWORD 80 
    #define MAXLEN  20 
    
    int tokenize(char *str, char **tokens)
      {
        char  *sep = strtok(str, DELIM);
        int i=0;
        while (sep != NULL)
        {
          strcpy(tokens[i++], sep);
          sep = strtok(NULL, DELIM);
        }
        return i;
      }
    
    
    int main(void)
    {
      char  parameters[MAXWORD][MAXLEN];
      char  toserver[BUFSIZ];
      int   ntokens = 0;
      int i;
      printf("Enter a string: ");
      fflush(stdout);
    
      if (fgets(toserver, sizeof toserver, stdin) != NULL)
    	ntokens=tokenize(toserver,parameters);
    
      for (i = 0; i < ntokens; i++) 
      {
        puts(parameters[i]);
      }
    
      return(0);
    }
    i want to store the tokens in the parameter array but i got a compilation error in line 31 saying "passing argument 2 of 'tokenize' from incompatible pointer type"

    what's wrong with the code?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    parameters is not a pointer to pointer to char; it was declared as a double array of char, and when used by itself it decays to a pointer to an array of char (decay only happens once, not recursively).

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're trying to pass the 2D array parameters[][], as a 1D array, in the call to tokenize().

    You have two asterisks in tokenize(), but what about the calling line of code?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    should i initialize parameters as char **parameters instead?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What does your instructor say he wants you to do?

    There's more than one way to do this. You can use pointers, or you can use index's, or you can even mix them a bit.

    It's best to follow what your teacher/prof is telling you to do. If you're following a book, then use the same style that the book is using, at that time.

    I'd stick with using indexes for beginners, rather than pointers (so, no). Later, you'll surely learn both.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    i want to make tokenize a function so that i could reuse it everytime there's a new string to be tokenized. i followed the code from the prelude but the tokenize mechanism is in the main function.

    for example i got another string and I need to tokenize it, i don't want to write
    Code:
        char  *sep = strtok(toserver, DELIM);
    
        while (sep != NULL)
        {
          strcpy(parameters[ntokens++], sep);
          sep = strtok(NULL, DELIM);
        }
    allover again.
    thanks/

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The main point is that you need to make the two agree. Arrays are (far) easier, so I would suggest making them both arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM