I'm trying to parse a string around an arbitrary index. In my simplest test program I could come up with I have an input string I read the input into and then do memcpy to parse the string.
For testing this I am typing in "this text" as input. readInput is a function where I just have it calling getline(&input, &size, stdnin) and return the input pointer.
int main(){
char *input;
input = readInput();
int parseAround = 4;
char *token1;
char *token2;
memcpy(token1, inputBuffer, 4);
printf("token: %s\n", token1); //prints "this"
memcpy(token1, inputBuffer + (parseAround+1), 4);
//when changed to memcpy(token2,...); segfaults
printf("token: %s\n", token1); //prints "text"
free(input);
return 0;
}
However when I change the second memcpy to use token2 rather than token1, I get a segmentation fault. Why is this?