简体   繁体   English

function 代号和实际调用是什么?

[英]What is function designator and actual call?

According to C99 Standard:根据 C99 标准:

The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call. function 指示符、实际 arguments 和实际 arguments 中的子表达式的评估顺序未指定,但在实际调用之前有一个序列点。

Could anyone explain clearly what is function designator and actual call;谁能清楚地解释什么是 function 指示符和实际调用; and what is difference between them?它们之间有什么区别?

A function designator is an expression with function type. function 指示符是具有 function 类型的表达式。 That is, when you type func(a,b);也就是说,当您键入func(a,b); to call a function, func is the function designator.调用 function, func是 function 指示符。

(Such a function designator when present in an expression normally "decays" into a pointer to function, much like arrays decay into a pointer to the first element.) (当出现在表达式中时,这样的 function 指示符通常“衰减”为指向 function 的指针,很像 arrays 衰减为指向第一个元素的指针。)

The meaning of "sequence point before the actual call" is that all operands must be fully evaluated (executed) before the function is called. “实际调用前的序列点”的意思是在调用 function 之前必须对所有操作数进行完全评估(执行)。 If you have func(a(), b()) , it is unspecified if a() or b() is executed first, but you know that both of them are definitely executed before func is called.如果您有func(a(), b()) ,则未指定是否先执行a()b() ,但您知道它们肯定会在func之前执行。

So if a() for example modified a global variable that is also used by func , that would be fine because the access to that variable would be sequenced in a well-defined order.因此,如果a()例如修改了一个也被func使用的全局变量,那很好,因为对该变量的访问将按照定义明确的顺序进行排序。

I would assume that the function designator is what tells the program which function to call.我假设 function 指示符告诉程序调用哪个function。 It might be either a function name or an expression resulting in a function pointer.它可能是 function 名称或导致 function 指针的表达式。

Edit:编辑:

Here is a function pointer example:这是一个 function 指针示例:

// Two random functions
int foo(int i);
int bar(int i);

// define a function pointer type
typedef int (* func)(int)

// Array of functions

func a[] = {foo, bar}


// calling bar(123)
// (a[1]) is an expression that evaluates to a pointer to `bar`

(a[1])(123);

" What is a function designator? " 什么是 function 代号?


A function designator is an expression that has function type. function 指示符是具有 function 类型的表达式。 Except when it is the operand of the sizeof operator, 66) or the unary & operator, a function designator with type "function returning type" is converted to an expression that has type "pointer to function returning type".除非它是 sizeof 运算符 66) 或一元 & 运算符的操作数,否则类型为“函数返回类型”的 function 指示符将转换为类型为“指向 function 返回类型的指针”的表达式。

66) Because this conversion does not occur, the operand of the sizeof operator remains a function designator and violates the constraints in 6.5.3.4. 66) 因为没有发生这种转换,sizeof 运算符的操作数仍然是 function 指示符,违反了 6.5.3.4 中的约束。

Source: C18, §6.3.2.1/4 - "Lvalues, arrays, and function designators"资料来源:C18,§6.3.2.1/4 -“左值、arrays 和 function 代号”


A function designator is an expression which identifies a certain function and that designates a function when evaluated. function 指示符是标识某个 function 并在评估时指定function 的表达式。

int tip (int);

tip (the name of the function) is for example a function designator. tip (函数的名称)例如是 function 指示符。

Or for example in:或者例如在:

#include <stdio.h>

int tip (int a) {
    return a;
}

int main (void) {

    int (**foo)(int);

    int (*bar[5])(int);

    bar[0] = &tip;

    foo = &bar;

    printf("%d", (**foo)(4));
}

The pointer to pointer to function returning int foo - (**foo)(4) - is a function designator for the function tip .指向指向 function 的指针的指针返回 int foo - (**foo)(4) - 是 function tip的 function 指示符。


" What is the function call? " function 调用是什么?

The function call is actually just the call to a function, like tip(4); function 调用实际上只是对 function 的调用,如tip(4); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM