int * (*) 的类型 (int * , int * (*)())

发布于 2024-08-22 02:46:53 字数 120 浏览 6 评论 0原文

int * (*) (int * , int * (*)())

我想知道它是什么类型? ,有人可以给出使用这种类型的声明的示例吗?

任何帮助都会很棒。

谢谢。

int * (*) (int * , int * (*)())

I'd like to know what type is it ? , can someone give an example of a declaration using this type.

any help would be great.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫性小仙女 2024-08-29 02:46:53

它是一个指向返回 int* 并接受 int* 的函数的指针,也是一个指向返回 int* 的函数的指针(并接受未定义数量的参数) ;见评论)。

一些示例(看起来不太好,它只是被构造为包含提到的声明):

#include <stdio.h>

static int a = 10;
int* f1() {
    return &a;
}

static int b;
int* f2(int *j, int*(*f)()) {
    b = *j + *f();
    // this is just for demonstrational purpose, such usage
    // of global variable makes this function not thread-safe
    return &b;
} 


int main(int argc, char *argv[]) {
    int * (*ptr1)();
    int * (*ptr2) (int * , int * (*)());
    ptr1 = f1;
    ptr2 = f2;

    int i = 42;
    int *pi = ptr2(&i, ptr1);
    printf("%d\n", *pi);

    return 0;
}

// prints 52

It is a pointer to function that returns int* and accepts int* and pointer to function that returns int* (and accepts undefined number of parameters; see comments).

Some example (does not look very nice, it is just constructed to contain the mentioned declaration):

#include <stdio.h>

static int a = 10;
int* f1() {
    return &a;
}

static int b;
int* f2(int *j, int*(*f)()) {
    b = *j + *f();
    // this is just for demonstrational purpose, such usage
    // of global variable makes this function not thread-safe
    return &b;
} 


int main(int argc, char *argv[]) {
    int * (*ptr1)();
    int * (*ptr2) (int * , int * (*)());
    ptr1 = f1;
    ptr2 = f2;

    int i = 42;
    int *pi = ptr2(&i, ptr1);
    printf("%d\n", *pi);

    return 0;
}

// prints 52
甜嗑 2024-08-29 02:46:53

cdecl 是你的朋友:

$ cdecl explain 'int * (*x) (int * , int * (*)())'
declare x as pointer to function (pointer to int, pointer to function returning pointer to int) returning pointer to int

cdecl is your friend:

$ cdecl explain 'int * (*x) (int * , int * (*)())'
declare x as pointer to function (pointer to int, pointer to function returning pointer to int) returning pointer to int
血之狂魔 2024-08-29 02:46:53

嗯...根据 cdecl.org 这是一个语法错误 - 让我尝试一下

int * (*) (int *,int *(*)())
  • (int , int ()()) - 最里面
    (
    )() - 函数指针
    int ()() - 指向无参数函数的指针,返回指向 int 的指针
  • (int *, ...) - 两个参数,一个是指向 int 的指针,另一个是指针 - to-function-with-no-parameters-returning-pointer-to-int
  • (*)(...) - 带有参数 int 的函数指针
  • * (*)(...) - 函数指针返回 -指向 int 的指针

所以:
它是一个函数指针,有两个参数,第一个参数是指向 int 的指针,另一个是无参数返回 int 指针的函数指针,其返回指针至-int。

编辑:我在该网站中使用的 C 声明 - 我没有放入返回的变量名称

int *(*x)(int *,int *(*)())

将 x 声明为指向函数的指针(指向 int 的指针、指向返回指向 int 的指针的函数)返回指向 int 的指针

Hmmm... according to cdecl.org that was a syntax error - let me try

int * (*) (int *,int *(*)())
  • (int , int ()()) - innermost
    (
    )() - a pointer to function
    int ()() - pointer to function with no parameters, returning pointer to int
  • (int *, ...) - two parameters, which one is a pointer to int, and the other is pointer-to-function-with-no-parameters-returning-pointer-to-int
  • (*)(...) - a function pointer with the parameters
  • int * (*)(...) - a function-pointer-returning-pointer-to-int

So:
It's a function-pointer which has the two parameters which the first parameter is a pointer to int and the other is pointer-to-function-with-no-parameters-returning-pointer-to-int,and its-returning-pointer-to-int.

Edit: The C declaration that I used in that website - I did not put in a variable name as in

int *(*x)(int *,int *(*)())

which returned: declare x as pointer to function (pointer to int, pointer to function returning pointer to int) returning pointer to int

初懵 2024-08-29 02:46:53

有一种称为“左右规则”的技术可以帮助您破译此类复杂的声明。该规则的工作原理是用英语关键字替换声明中出现的属性。然后,当您将关键字放在一起时,构建的句子将描述声明。

以下是您应该使用的属性和关键字:

  • 当您看到属性“()”时,请使用关键字“返回的函数”
  • 当您看到属性“[n]”时,请使用关键字“array of n”
  • 当您看到属性“*”时” use 关键字“pointer to”

现在这里是“右左规则”:

  1. 从标识符开始。
  2. 查看右侧的属性。
  3. 如果没有找到,请向左看。
  4. 找到属性后,替换其英文关键字。
  5. 当你走出去时,继续从右到左的替换。
  6. 当到达声明中的数据类型时停止。

下面是一些示例:

int n[10];

标识符是 n。右侧的属性是[10],因此使用关键字“array of 10”。接下来,您将到达数据类型 int。因此,

n 是一个“10 个整数的数组”。

int *n[10];

标识符是n。右侧的属性是[10],因此使用关键字“array of 10”。向左看,属性是*,所以使用关键字“pointer to”。没有更多的属性了。剩下的就是数据类型,即int。将关键字放在一起可以得到:

n 是一个“由 10 个整数指针组成的数组”。

int (*pf)();

标识符是pf。 pf 右侧没有紧邻的属性。 pf 左边是*。所以第一个关键字是“指向的指针”。接下来回到右边,属性是()。这意味着下一个关键字是“返回的函数”。现在回到左边的数据类型int。将关键字放在一起可以得到:

pf 是“指向返回 int 的函数的指针”

int *(*pf)();

pf 是标识符。 pf 右边没有属性。左边是*,所以第一个关键字是“pointer to”。回到右边是(),所以下一个关键字是“返回的函数”。回到左边是*,所以下一个关键字是“pointer to”。接下来,到达 int 数据类型:

pf 是“指向返回 int 指针的函数的指针”。

下一个示例与上一个示例类似,但这次 pf 函数有一些参数。参数为 int *xint *(*y)()。您应该能够根据到目前为止的所有内容来描述每个论点。一旦你这样做了,你将能够描述整个事情:

int *(*pf)(int *x, int *(*y)());

pf 是一个指向函数的指针,该函数返回一个指向 int 的指针。 pf 有两个参数。第一个参数 x 是一个指向 int 的指针。第二个参数 y 是一个指向函数的指针,该函数返回一个指向 int 的指针。

There's a technique called the "right-left rule" that can help you decipher complex declarations like these. The rule works by substituting english keywords for the attributes that appear in the declaration. Then when you put the keywords together, the sentence constructed will describe the declaration.

Here's the attributes and the keywords you should use:

  • When you see the attribute "()" use keyword "function that returns"
  • When you see the attribute "[n]" use keyword "array of n"
  • When you see the attribute "*" use keyword "pointer to"

Now here's the "right-left rule":

  1. Start with the identifier.
  2. Look to the right for an attribute.
  3. If none is found, look to the left.
  4. Once an attribute is found, substitute its English keyword.
  5. Continue right-left substitutions as you work your way out.
  6. Stop when you've reached the data type in the declaration.

Here's some examples:

int n[10];

The identifier is n. The attribute on the right is [10], so use the keyword "array of 10". Next you reach the data type int. So,

n is an "array of 10 integers".

int *n[10];

The identifier is n. The attribute on the right is [10], so use the keyword "array of 10". Look to the left and the attribute is * so use keyword "pointer to". There's no more attributes. All that is left is the data type, which is int. Put the keywords together to get:

n is an "array of 10 pointers to integers".

int (*pf)();

The identifier is pf. There's no attribute immediately to the right of pf. To the left of pf is *. So the first keyword is "pointer to". Next, go back to the right and the attribute is (). That means the next keyword is "function that returns". Now go back to the left to the data type int. Put the keywords together to get:

pf is a "pointer to a function that returns an int"

int *(*pf)();

pf is the identifier. There's no attributes to the right of pf. To the left is *, so the first keyword is "pointer to". Back to the right is (), so the next keyword is "function that returns". Back to the left is *, so the next keyword is "pointer to". Next, reach the int data type:

pf is a "pointer to a function that returns a pointer to an int".

This next example is just like the previous one, but this time there's some arguments to the pf function. The arguments are int *x and int *(*y)(). You should be able to describe each of these arguments based on the everything up until now. And once you do that you'll be able to describe the whole thing:

int *(*pf)(int *x, int *(*y)());

pf is a pointer to a function that returns a pointer to an int. pf takes two arguments. The first argument x is a pointer to an int. The second argument y is a pointer to a function that returns a pointer to an int.

葬花如无物 2024-08-29 02:46:53
typedef int* (*fptr)();    
int* foo(int* p1, fptr p2);

您可以将 foo 放入该类型中。

typedef int* (*fptr)();    
int* foo(int* p1, fptr p2);

You can put foo in that type.

煮茶煮酒煮时光 2024-08-29 02:46:53

这样的声明确实有用!考虑标准 C 库的信号函数:

void (*
     signal(int sig, void (*func)(int)))(int);

信号手册页解释说它相当于以下 typedef 版本:

typedef void (*sig_t) (int);
sig_t signal(int sig, sig_t func);

一个接受两个参数、int 和 sig_t 函数的函数,并返回旧的 sig 函数。

Such declaration are really used !. Consider the signal function of the standard C library:

void (*
     signal(int sig, void (*func)(int)))(int);

the signal man page explains it is equivalent to the following typedef'd version:

typedef void (*sig_t) (int);
sig_t signal(int sig, sig_t func);

A function that takes two args, and int and a sig_t function, and that returns the old sig function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文