Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指向函数的指针_C_Function_Pointers - Fatal编程技术网

C 指向函数的指针

C 指向函数的指针,c,function,pointers,C,Function,Pointers,如何定义名为的变量,该变量是指向函数的指针,该函数将一个指针类型的参数指针指向double,并返回指向short的指针 这是正确的吗? short*(*吓人)(double*)首先,你应该在谷歌上搜索“函数指针C”,但我将把你的问题作为你已经学习的内容的确认 是的,这是正确的,以下面的例子为例: short global = 2; short * ptr_to_global = &global; short* scary_fun(double* ptr) { return pt

如何定义名为的变量,该变量是指向函数的指针,该函数将一个指针类型的参数指针指向double,并返回指向short的指针

这是正确的吗?
short*(*吓人)(double*)
首先,你应该在谷歌上搜索“函数指针C”,但我将把你的问题作为你已经学习的内容的确认

是的,这是正确的,以下面的例子为例:

short global = 2;
short * ptr_to_global = &global;

short* scary_fun(double* ptr) {
    return ptr_to_global;
}

int main(void) {
    double val = 22.0;
    double *ptr_to_val = &val;
    short* (*scary)(double*);
    scary = &scary_fun;
    printf("%d", *(scary(ptr_to_val))); // Prints "2"
    return 0;
}

下标
[]
和函数调用
()
运算符的优先级高于一元
*
,因此:

T *a[N]    -- a is an N-element array of pointer to T
T (*a)[N]  -- a is a pointer to an N-element array of T
T *f()     -- f is a function retunring pointer to T
T (*f)()   -- f is a pointer to a function returning T

如果有一种方法来测试的话…我想说的是你不应该这么做。
T *a[N]    -- a is an N-element array of pointer to T
T (*a)[N]  -- a is a pointer to an N-element array of T
T *f()     -- f is a function retunring pointer to T
T (*f)()   -- f is a pointer to a function returning T