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.