在C语言中,函数名代表什么?

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

What does a function name stand for in C

问题

编译器在我们定义函数时做什么?函数名称是指针吗?还是其他什么东西?

我希望我了解一些有关函数名称的知识。

英文:

What does the compiler do when we define a function? And is the function name a pointer? Or maybe it's something else.

I wish I knew something about function names.

答案1

得分: 4

以下是翻译好的内容:

What does a function name stand for in C

函数名代表函数本身。

What does the compiler do when we define a function?

编译器记住函数的名称,并生成实现函数的代码。它还会记住函数声明中的信息,例如返回类型和参数的类型,如果使用了原型形式。

And is the function name a pointer? Or maybe it's something else.

函数的名称指代函数本身。

当在表达式中使用函数名时,它会自动转换为指向函数的指针,但有一些例外情况,稍后会讨论。此外,函数名实际上唯一的用途就是作为指向函数的指针。因此,程序员可能认为函数名是指针,但它实际上不是指针。

自动转换的一个例外是在函数名与 sizeof 一起使用时。然而,在函数上使用 sizeof 通常是错误的(它没有被C标准定义,只能在C实现将其定义为扩展时工作),因此这个例外在普通实践中很少被使用。另一个例外是当函数名与一元 & 一起使用,就像 &sin 一样。在这种情况下,我们明确地取函数的地址,从而产生一个指向函数的指针,因此不需要自动转换为指向函数的指针。

当调用函数时,就像 sin(x) 一样,"函数调用运算符",( … ),以函数的指针作为其第一个操作数。因此,由于自动转换,sin(x) 实际上是 (&sin)(x),这表示调用其地址为 &sin 的函数并传递参数 x

函数调用运算符接受函数指针,这就是我们可以将指针用于函数的原因,例如 double (*p)(double) = sin; printf("sin(.5) = %g.\n", p(.5));

英文:

> What does a function name stand for in C

The function name stands for the function.

> What does the compiler do when we define a function?

The compiler remembers the name of the function, and it generates code to implement the function. It also remembers information from the declaration of the function, such as the return type and the types of the parameters, if a prototype form was used.

> And is the function name a pointer? Or maybe it's something else.

The name of a function designates a function.

When a function name is used in an expression, it is automatically converted to a pointer to the function, with a couple of exceptions discussed below. Furthermore, effectively the only use for a function name is as a pointer to the function. Because of this, a programmer might think of the function name as a pointer, but it is not actually a pointer.

One exception to the automatic conversion is when a function name is used with sizeof. However, using sizeof on a function is usually an error (it is not defined by the C standard and can work only if a C implementation defines it as an extension), so this exception is never used in ordinary practice. The other exception is when a function name is used with unary &, as in &sin. In this case, we are explicitly taking the address of the function, which produces a pointer to the function, so there is no need for automatic conversion to a pointer to the function.

When you call a function, as with sin(x), the “function-call operator,” ( … ), takes a pointer to the function as its first operand. So, due to the automatic conversion, sin(x) is effectively (&sin)(x), which says to call the function whose address is &sin and pass it the argument x.

The fact that the function-call operator takes a pointer to a function is why we can use pointers for functions, as in double (*p)(double) = sin; printf("sin(.5) = %g.\n", p(.5));.

huangapple
  • 本文由 发表于 2023年2月14日 18:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446778.html
匿名

发表评论

匿名网友

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

确定