I have a global variable declared char* global=NULL and a function where I parse a separate string but copy its value into the global variable. Essentially copying the second word in the string into the global variable. And at the end of this function in the last print statement it prints out the global variable correctly, which again contains the second word of the string.
void parsestring(char* s, char** ssp){
int i;
char st[30];
strcpy(st,s);
printf("String st after copy is %s",st);
char* first=strtok(st," ");
char* second=strtok(NULL," ");
printf("\nstring s at the end is %s\n",s);
global=second;
printf("\nsecond after assigning to blobal is is %s\n",second);
printf("\n global at end of parse function is %s",global);
}
But when I call this global variable from a different function and test print its value, it only prints part of the string, not the entirety of it like it does at the end of the above function.
I know what i'm doing isn't optimal but I don't get why the global variable changes when called later in my program.