简体   繁体   English

int(abc)(int,int)是什么意思?

[英]What does int (abc)(int,int) mean?

I am trying some random declarations of array pointers and function pointers together. 我正在尝试一些随机声明的数组指针和函数指针。 I was struck here. 我被震惊了。

#include<bits/stdc++.h>
int add(int a,int b){ 
    return 1729; }
int main(){
    int (abc)(int,int);
    abc = add;
    return 0;
}

The emitted error is: 发出的错误是:

cannot convert 'int(int, int)' to 'int(int, int)' in assignment

But as both abc and add are of the same type, why should it convert? 但由于abcadd都属于同一类型,为什么要转换? Or is it possible to assign functions to new variables? 或者是否可以为新变量分配函数? If so, how to do that? 如果是这样,怎么办?

int (abc)(int,int);

It's not a function pointer. 它不是函数指针。 The extra parentheses don't matter in this case, and it's equivalent to 在这种情况下,额外的括号并不重要,它等同于

int abc(int,int);

Which is a function declaration , that C++ allows you to put almost anywhere (like the body of another function). 这是一个函数声明 ,C ++允许你几乎放在任何地方(比如另一个函数的主体)。 Your code doesn't compile because you attempt to assign one function name to another. 您的代码无法编译,因为您尝试将一个函数名称分配给另一个函数名称。

To get a pointer, you need pointer syntax 要获取指针,您需要指针语法

int (*abc)(int,int);

Where, this time, the parentheses are required, else you get a function declaration with an int* return type! 这一次,括号必需的,否则你得到一个带有int*返回类型的函数声明! And now the assignment will be well-formed. 现在,任务将很好。

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

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