在C语言中,我们能否在现有函数内声明和定义另一个函数?

huangapple go评论56阅读模式
英文:

Can we declare and define a function inside an existing function in C?

问题

void function(int entry, int x1, int x2)
{
	void (*new_func)(int n1, int n2);
	new_func = (void (*)(int, int)) entry;
	
	new_func(x1, x2);
}

这段代码定义了一个名为 function 的函数,其中有一个名为 new_func 的函数指针,它将传入的 entry 转换为一个函数指针,并调用这个函数指针,传入参数 x1x2。在代码编译时似乎没有问题,但在调试时却出现了问题,导致程序退出。

英文:
void function(int entry, int x1, int x2)
{
	void (*new_func)(int n1, int n2);
	new_func = (void (*)(int, int)) entry;
	
	new_func(x1,x2);
	
}

Lets say we have this function, there is a function new_func which is declared and defined and called from within the function.
Is this allowed ? Is this possible ?

This code is compiling but while debugging it seems to be exiting.

答案1

得分: 1

嵌套函数不是标准的一部分。然而,一些编译器支持这个作为扩展,比如 gcc。在这里阅读更多关于C中嵌套函数的信息:https://stackoverflow.com/q/2608158/6699433

然而,在你的代码中,这并不是在发生的情况。在你的代码中,它是一个函数指针。在这里阅读更多相关信息:https://stackoverflow.com/q/840501/6699433

而且在没有看到整个代码的情况下,我会说 entry 可能有错误的类型。它可能应该是 intptr_tuintptr_t。https://stackoverflow.com/q/1845482/6699433

英文:

Nested functions are not a part of the standard. However, some compilers support this as extensions. Like gcc. Read more about nested functions in C here https://stackoverflow.com/q/2608158/6699433

However, that's not what's going on here. In your code it's a function pointer. Read more about that here: https://stackoverflow.com/q/840501/6699433

And without seeing the whole code, I'd say that entry probably has wrong type. It should probably be intptr_t or uintptr_t. https://stackoverflow.com/q/1845482/6699433

答案2

得分: 1

以下是翻译好的部分:

首先,new_func 不是一个函数,而是一个函数指针。

你在问题中展示的代码片段将一个 int 值转换为类型为 void (*)(int, int) 的函数指针,并将其赋给了 new_func

new_func = (void (*)(int, int)) entry;

但你没有提到 function() 函数的 entry 参数传递了什么参数。你是将一个函数指针强制转换为 int 并将其作为 function() 函数的第一个参数吗?

你的程序可能会具有实现定义的行为或未定义的行为,这取决于传递给 function() 函数的第一个参数的值。

根据 C11#6.3.2.3p5

5 可以将整数转换为任何指针类型。除非先前指定,否则结果是实现定义的,可能没有正确对齐,可能不指向引用类型的实体,可能是陷阱表示。67)

根据 C11#6.3.2.3p6

6 任何指针类型都可以转换为整数类型。除非先前指定,否则结果是实现定义的。如果结果不能表示为整数类型,行为是未定义的。结果不必在任何整数类型的值范围内。

英文:

First of all, new_func is not a function, it's a function pointer.

The code snippet that you have shown in your question is casting an int value to a function pointer of type void (*)(int, int) and assigning it to new_func:

new_func = (void (*)(int, int)) entry;

but you haven't mentioned what argument is passed to entry parameter of function() function.
Are you type casting a function pointer to int and passing it as first argument to function function()?

Your program can either have implementation defined behaviour or undefined behaviour depending on value of first argument passed function() function.

From C11#6.3.2.3p5
>5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.67)

From C11#6.3.2.3p6
> 6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

huangapple
  • 本文由 发表于 2023年4月11日 15:31:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定