The value of yytext
gets overwritten. You will need to duplicate and copy the value rather than simply assigning it:
longest = malloc(sizeof(char)*strlen(yytext)); strcpy(longest, yytext);
Keep in mind that you will want to cleanup (free) successive values of longest
. But be careful to not free
the statically assigned value longest = "";
!