Thread: strtok problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    strtok problem

    Hello all,

    I'm trying to do my homework and have run into an odd problem. What I need to do is take a 2 part command from the command line, and seperate it into 2 seperate strings. I'm trying to use strtok to split the command on a space, but for some reason it keeps throwing an access violation in the strtok.c file at the point where it starts parsing the string. I've searched everything, and can't find any reason why it won't work, my code is similiar to other bits I've found here.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void whichCommand();
    
    int main()
    {
                   whichCommand();
    
                   return 0;
    }
    
    void whichCommand()
    {
    	char** args = NULL;
    	char* args2 = NULL;
    	char *delim = " ";
    	char *result = NULL;
    
    	fgets(&args2,40,stdin);
    	printf("%s", &args2);
    	result = strtok(args2, delim);
    	printf("DID IT");
    	args[0] = result;
    	result = strtok(NULL, delim);
    	args[1] = result;
    	printf("%s", &args[0]);
    	printf("%s", &args[1]);
    	//exeCommand(args);
    }
    It chokes on the first call to strtok, never gets to the printf("DID IT"); statement. I'm not entirely sure, it may not like the way I handle the 2d array once I get it to split the string, but I haven't gotten far enough to worry about that yet

    Any help is appriciated, as I cannot figure this out, and the other methods I attempted to use to split the string turned out alot worse.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm confused. Your compiler isn't throwing any warnings? fgets takes a char * in its first argument, but you're giving it a char** (&args2 is the address of a char *, not a char * itself). Same with printf. Notice that once you start using args2 correctly (in strtok) things start getting a little weird. Try using it correctly all the way through.

    I also have no idea why you think there's room for 40 characters at the other end of args2, when you explicitly make sure that there's no room at all. You need to do something like
    Code:
    args2 = malloc(40);
    so that you have forty bytes to work with.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Quote Originally Posted by tabstop View Post
    I'm confused. Your compiler isn't throwing any warnings? fgets takes a char * in its first argument, but you're giving it a char** (&args2 is the address of a char *, not a char * itself). Same with printf. Notice that once you start using args2 correctly (in strtok) things start getting a little weird. Try using it correctly all the way through.

    I also have no idea why you think there's room for 40 characters at the other end of args2, when you explicitly make sure that there's no room at all. You need to do something like
    Code:
    args2 = malloc(40);
    so that you have forty bytes to work with.
    That did the trick, thank you. Was never formally intoduced to malloc, seen it used a few times, but none of my prof's ever covered it. This is actually the first time I've ever bumped heads with it, and once I get this program done I'm gonna have to google it and get a handle on it.

    Everythings working fine now, just have to go figure out how to trim the trailing character fgets seems to be putting on at the end, I'm sure I can figure that out pretty easily.

    Thanks again!

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by Gatt9 View Post
    Everythings working fine now, just have to go figure out how to trim the trailing character fgets seems to be putting on at the end, I'm sure I can figure that out pretty easily.
    Thanks again!
    Check out the following post from this forum.
    http://cboard.cprogramming.com/showp...2&postcount=14
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM