有谁知道这是什么“ typedef long long int(stoll_t)(const char *,char **,int);”?

时间:2019-02-16 17:31:26

标签: c c++14

我正在检查代码库,发现了这个

typedef long long int (stoll_t)(const char *, char **, int); 

不知道它做什么? 以及如何调用此函数?

它在代码中的样子

long long int argtoll( const char *str, const char **end, stoll_t stoll); //this 

2 个答案:

答案 0 :(得分:1)

typedef long long int (stow)(const char *, char **, int);stow定义为一种类型。

该类型为function (pointer to const char, pointer to pointer to char, int) returning long long int

long long int example_function_of_that_type(const char *a, char **b, int c) {
  if (a == NULL) return 1;
  if (b == NULL) return 2;
  return c;
}

答案 1 :(得分:-1)

它定义了函数类型的别名

它的作用类似于extern函数声明的较短版本。

示例:

#include <stdio.h>

typedef int (stow)( int);

int main(void) {
    // your code goes here
    stow x;

    printf("%d\n", x(5));
    return 0;
}

x必须在其他(或同一编译单元)中定义。

几乎无用(至少我找不到它的任何实际用途)-但绝对可以使代码更难阅读。因此,唯一的用途是对代码进行混淆或不隐藏函数指针。

stow *y;-不会在typedef中隐藏函数指针。

大多数程序员实际上更喜欢对函数的指针进行typedef定义。

stow argtoll;

argtoll(/*... actual parameters */);