一个人如何申报常数C函数指针数组? [英] How does one declare an array of constant function pointers in C?

查看:148
本文介绍了一个人如何申报常数C函数指针数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要声明指针数组的功能,像这样:

I need to declare an array of pointers to functions like so:

extern void function1(void);
extern void function2(void);
...

void (*MESSAGE_HANDLERS[])(void) = {
   function1,
   function2,
   ...
};

但是,我想的阵列被声明为常数 - 两个在阵列中的数据和指向数据的指针。不幸的是,我不记得在哪里放置const的键字(S)。

However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do not recall where to place the const key-word(s).

我假定实际指针,MESSAGE_HANDLERS在这种情况下,因为它被声明为数组已经恒定。在otherhand,不能在数组中的函数指针在运行时的变化,如果它被声明为显示?

I'm assuming the actual pointer, MESSAGE_HANDLERS in this case, is already constant because it is declared as an array. On the otherhand, couldn't the function pointers within the array be change at runtime if it is declared as shown?

推荐答案

有是要记住如何建立这种类型的技术。第一次尝试读取他们的名字开始指针和从右至左读。

There is a technique to remember how to build such type. First try to read pointers starting from their name and read from right to left.

T t[5];

为5吨的的的阵列。使t的函数的类型,则写回式向左,和参数向右

is an array of 5 T. To make T a function type, you write the return-type to the left, and the parameters to the right:

void t[5](void);

5阵列功能返回void,并采取任何参数的。但是功能本身不能在阵列酿!他们不是对象。只有指向他们可以。

would be an array of 5 functions returning void and taking no parameters. But functions itself can't be stuffed in arrays! They are not objects. Only pointers to them can.

关于

void * t[5](void);

这仍然是错误的,因为它只是改变返回类型是一个指针无效。您必须使用括号:

That's still wrong as it would just change the return-type to be a pointer to void. You have to use parentheses:

void (*t[5])(void);

,这将实际工作。的 t为5指针数组给函数返回void,并采取任何参数的。

大!关于指针数组怎么阿拉斯?这是非常相似的。元素类型出现在左侧,并在合适的尺寸。此外,还需要括号,因为否则阵列将成为整数指针多维数组:

Great! What about an array of pointers to arras? That's very similar. The element type appears at the left, and the dimension at the right. Again, parentheses are needed because otherwise the array would become a multidimensional array of integer pointers:

int (*t[5])[3];

这就是它!一个的 5指针数组3整型数组的。

我们刚刚得知是关于函数真了。让我们来声明函数采取这一返回一个指针给另一个函数采取任何参数并返回void一个int:

What we have just learned is true about functions too. Let's declare a function taking an int that returns a pointer to another function taking no parameter and returning void:

void (*f(int))(void);

我们需要再次括号如上他一样的道理。现在,我们可以调用它,并调用函数返回再次指向。

we need parentheses again for he same reason as above. We could now call it, and call the returned function pointed to again.

f(10)();

返回一个指针函数返回另一个函数指针

这个是什么?

f(10)(true)(3.4);

?换句话说,一个的功能将如何采取int类型的指针,返回的函数取布尔返回一个指针的函数采取双人和返回void 的会是什么样子?答案是,你只是窝他们:

? In other words, how would a function taking int returning a pointer to a function taking bool returning a pointer to a function taking double and returning void would look like? The answer is that you just nest them:

void (*(*f(int))(bool))(double);

您可以这样做无尽的时间。事实上,你也可以返回一个指向数组的指针就像你可以指向一个功能:

You could do so endless times. Indeed, you can also return a pointer to an array just like you can a pointer to a function:

int (*(*f(int))(bool))[3];

这是采取int类型的指针返回到函数取布尔指针返回3一个int数组

This is a function taking int returning a pointer to a function taking bool returning a pointer to an array of 3 int

现在,上面解释了如何建立从基本类型络合剂的类型,你可以在地方,你现在知道他们是属于把常量。试想:

Now that the above explained how to build up complexer types from fundamental types, you can put const at places where you now know where they belong to. Just consider:

T c * c * c ... * c name;

T 是,我们最终在最后指向的基本类型。在 C 表示无论是const或不常量。例如:

The T is the basic type that we end up pointing to at the end. The c stands for either const or not const. For example

int const * const * name;

将宣布名称有型的指向一个常量指针,以一个恒定的INT 的。您可以更改名称,但你不能改变 *名称,这将是类型

will declare name to have the type pointer to a constant pointer to a constant int. You can change name, but you cannot change *name, which would be of type

int const * const

和既不 **名称,这将是类型

int const

让我们这适用于上面的函数指针:

Let's apply this to a function pointer of above:

void (* const t[5])(void);

这实际上将声明数组包含常数指针。因此,创建(和初始化)阵列之后,的指针的是常量,因为常量星后出现。需要注意的是,我们不能把一个常量星号之前在这种情况下,由于有没有指针不断的功能即可。功能简单地说就没有意义不能是const。所以下面是无效的:

This would actually declare the array to contain constant pointers. So after creating (and initializing) the array, the pointers are const, because the const appeared after the star. Note that we cannot put a const before the star in this case, since there are no pointers to constant functions. Functions simply can't be const as that would not make sense. So the following is not valid:

void (const * t[5])(void);

结论

声明函数和数组实际的C ++和C的方式实际上是一个有点混乱。你必须先得到你的头左右,但如果你了解它,你可以用它写的非常紧凑的函数声明。

Conclusion

The C++ and C way of declaring functions and arrays actually is actually a bit confusing. You have to get your head around it first, but if you understand it, you can write very compact function declarations using it.

这篇关于一个人如何申报常数C函数指针数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆