一个函数里两个setjmp_C语言中setjmp.h的longjmp()函数

一个函数里两个setjmp

We can call this function as an advance version of goto statement but with more dynamic range. The longjump() function allows us to pass parameters to know that the control has been jumped or not.

我们可以将此函数称为goto语句的高级版本,但具有更大的动态范围。 longjump()函数使我们可以传递参数来知道控件是否已跳转。

So how to use this is the question. First thing is to decide point from where you want to jump and then decide to where you want to jump.

因此,如何使用此问题。 第一件事是确定要从哪里跳转的点,然后再确定要跳转的位置。

Before setting these point just make a jum_buf object. Now rest is a cake walk. In this example, I have put the point from where we want to jump inside a function called func by calling the function longjmp(a, 1) with two parameters as the jum_buf object and 1 will be return at the setjump call.

在设置这些点之前,只需创建一个jum_buf对象。 现在休息是轻轻松松。 在此示例中,我通过调用带有两个参数的longjmp(a,1)函数作为jum_buf对象,将要从其中跳转到函数func的位置,在setjump调用中将返回1。

Call the function setjmp() at the point to where you want to jump. The Second parameter from the longjump will be stored in z. This z can be then checked for loops or maybe something else.

在要跳转的位置调用函数setjmp() 。 longjump中的第二个参数将存储在z中 。 然后可以检查此z的循环或其他内容。

setjmp.h- longjmp()函数在C中的示例 (setjmp.h- longjmp() function Example in C)

</ s> </ s> </ s>
#include <stdio.h>
#include <setjmp.h>

//defining the type of the variable
static jmp_buf a;

void func(void)
{
	//message for user
	printf("Function starts here..\n");

	//calling function
	longjmp(a, 1);

	//message for user
	printf("Function ends here..\n");
}

int main()
{
	int z;

	//message for user
	printf("Main starts here..\n");

	//setting current value in z
	z = setjmp(a);

	//condition to display message
	if (z != 0)
	{
		//message for user
		printf("longjmp function called\n");
		return 0;
	}
	func();

	//message for user
	printf("Main ends here..\n");

	return 0;
}

Output

输出量

setjmp.h - longjmp() in c language

翻译自: https://www.includehelp.com/c-programs/longjmp-function-of-setjmp-h-in-c.aspx

一个函数里两个setjmp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值