#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf location; // Global variable
void function(void)
{
printf("About to longjmp\n");
longjmp(location, 1); // Return 1
}
int main(void)
{
if (setjmp(location) != 0) // Save the current location
{
printf("Returning from longjmp\n");
getchar();
exit(1);
}
function();
printf("hello");
getchar();
return 0;
}
本文介绍了一个使用C语言中的longjmp与setjmp函数的例子。通过一个简单的程序演示了如何利用这两个函数实现非局部跳转,即从一个函数跳转到另一个函数中保存的位置。
274

被折叠的 条评论
为什么被折叠?



