Thread: strtok & strcpy

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Question strtok & strcpy

    I'm using the strtok & strcpy for the program I'm doing. It's sort of a parser of polynomial equations. The strtok function works fine. However, when I try to add the strcpy function to save the tokens in arrays, the program compiles but the exe file sort of crashes and closes.

    Here's a sample code of what I'm working on.

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h> 
    #include<string.h>
    #include<stdlib.h>
    #define max 50
    
    main()
    {
         char string[max];
          
         printf("Input string: ");
         scanf("%s", &string);
    
         char tokens[max];
         char *token = NULL;
         int k;
         int i=0;
         token = strtok(string, " ^+-.x");
    
         while(token != '\0')
         {
              printf("%s\n", token);
              strcpy(&tokens[i++], token);        
              token = strtok(NULL, " ^+-.x");
         }
         
         printf("\n\n");
         
         for(k=0; k<=i; k++)
         {
         printf("%s\n", &tokens[k]);
         }
         
         getch();
         return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    your tokens array should be tokens[STRINGS][MAXLENGTH] ... eg: tokens[10][50] will hold 10 strings each 50 characters long. Also you don't need the & in strcpy... tokens is already a pointer.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    in the strcpy function, how would the array argument look like?
    Will I change it to something like this? since it's already a 2D array

    strcpy(tokens[i++][k++], token)

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by firehydrant View Post
    in the strcpy function, how would the array argument look like?
    Will I change it to something like this? since it's already a 2D array

    strcpy(tokens[i++][k++], token)
    No... just use tokens[i++] In the case of strings, the second dimension is not needed. Strings are null terminated so the strcpy() function will discover their length automatically.

    char tokens[max] is a single string... not an array of strings. That's why you need a 2d array.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    I got it already. Thanks! I didn't change the array argument in the strcpy function. Why did it work? Doesn't the initialization --tokens[STRINGS][MAXLENGTH]-- mean that it's a 2D array?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... a single string is an array of characters... eg char x[20]; stores 19 characters.
    If you want an array of multiple strings you are essentially creating an array of arrays of characters. So you need to go the 2d route... eg. char x[10][20]; stores 10 lines of 19 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok twice means problems!
    By tanjinjack in forum C Programming
    Replies: 8
    Last Post: 12-12-2010, 01:53 AM
  2. loading 2 character arrays in a row
    By rivkyfried1 in forum C Programming
    Replies: 4
    Last Post: 12-09-2010, 10:40 AM
  3. strcpy and strtok
    By sababa.sababa in forum C Programming
    Replies: 7
    Last Post: 12-10-2009, 04:37 PM
  4. strtok() and strcpy
    By grimlark in forum C Programming
    Replies: 7
    Last Post: 05-27-2009, 03:11 AM
  5. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM