I wrote the following code in order to understand setjmp and longjmp functions but I think the issue is unrelated to those functions. I am expecting the output to be:
function1
function2
function2
but I keep getting:
function1
function2
function1
as the out put. Code:
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
void f1(char * a);
void f2(char * a);
jmp_buf buf1;
int main(int argc, char *argv[])
{
char * w;
f1( w);
return 0;
}
void f1(char * a)
{
a = "funtion 1";
printf("%s\n",a);
int i = setjmp( buf1 );
if( i == 0 )
f2( a );
printf("%s\n", a);
}
void f2(char * a)
{
a = "function 2";
printf("%s\n",a);
longjmp( buf1 , 2 );
}
What am I doing wrong here ? Thanks for any help.