我需要翻译的部分是:”Do I have to initialize function pointers one by one?”

huangapple go评论86阅读模式
英文:

Do I have to initialize function pointers one by one?

问题

以下是翻译好的部分:

当我像下面这样一次性初始化函数指针时,它不起作用。

  1. ptr[3]={add, subtract, multiply};

这会导致:

[错误] 在“{”标记之前需要表达式

然而,逐个初始化可以正常工作。为什么会这样呢?

  1. // 函数指针数组
  2. #include <stdio.h>
  3. void add(int a, int b){
  4. printf("%d\n", a+b);
  5. }
  6. void subtract(int a, int b){
  7. printf("%d\n", a-b);
  8. }
  9. void multiply(int a, int b){
  10. printf("%d\n", a*b);
  11. }
  12. int main(){
  13. void (*ptr[3])(int, int);
  14. // ptr[3]={add, subtract, multiply}; 这种初始化方式不起作用
  15. // 但这种方式可以正常工作
  16. ptr[0]=add;
  17. ptr[1]=subtract;
  18. ptr[2]=multiply;
  19. ptr[2](3,5); // 15
  20. }
英文:

When I initialize function pointers in one take, like below, it does not work.

  1. ptr[3]={add, subtract, multiply};

This gives:

> [Error] expected expression before '{' token

However, one-by-one initialization works. Why is this?

  1. //array of function pointers
  2. #include&lt;stdio.h&gt;
  3. void add(int a, int b){
  4. printf(&quot;%d\n&quot;, a+b);
  5. }
  6. void subtract(int a, int b){
  7. printf(&quot;%d\n&quot;, a-b);
  8. }
  9. void multiply(int a, int b){
  10. printf(&quot;%d\n&quot;, a*b);
  11. }
  12. int main(){
  13. void (*ptr[3])(int, int);
  14. //ptr[3]={add, subtract, multiply}; this initialization does not work
  15. //but this works
  16. ptr[0]=add;
  17. ptr[1]=subtract;
  18. ptr[2]=multiply;
  19. ptr[2](3,5); //15
  20. }

答案1

得分: 2

在赋值语句中,ptr[3] = {add, subtract, multiply}; 右侧是(正确的)适合初始化包含三个函数指针的数组的初始化列表。然而,左侧(ptr[3])是错误的:那只是数组的一个单独元素,而且是一个越界的元素。

只需在声明中执行这个“赋值”操作,并将其初始化:

  1. int main(void)
  2. {
  3. void (*ptr[3])(int, int) = {add, subtract, multiply}; // 这种初始化是有效的
  4. ptr[2](3, 5); //15
  5. }

实际上,与您的数组的元素是函数指针相关的事情并没有什么特别之处。在除了声明之外的任何地方,没有数组可以一次性“赋值”(使用=运算符)。在变量声明中,= 符号的使用在形式上不是一个赋值操作;它是一个初始化操作。有用的阅读链接:https://stackoverflow.com/q/35662831/10871073。

英文:

In the assignment, ptr[3]={add, subtract, multiply}; the RHS is (correctly) a suitable initializer-list for an array of three function pointers. However, the LHS (ptr[3]) is wrong: that's just a single element of an array, and an out-of-bounds element, at that.

Just do the 'assignment' in the declaration, and make it an initialisation:

  1. int main(void)
  2. {
  3. void (*ptr[3])(int, int) = {add, subtract, multiply}; // this initialization does work
  4. ptr[2](3, 5); //15
  5. }

There is actually nothing special, here, related to the fact that your array's elements are function pointers. No array can be "assigned to" (using the = operator) en bloc, at any point other than in its declaration. In a variable declaration, the use of the = token isn't, formally, an assignment operation; it is an initialisation. Useful reading: https://stackoverflow.com/q/35662831/10871073.

答案2

得分: 1

你需要在声明时进行初始化。

  1. // 函数指针数组
  2. #include <stdio.h>
  3. void add(int a, int b){
  4. printf("%d\n", a+b);
  5. }
  6. void subtract(int a, int b){
  7. printf("%d\n", a-b);
  8. }
  9. void multiply(int a, int b){
  10. printf("%d\n", a*b);
  11. }
  12. int main(){
  13. void (*ptr[3])(int, int) = {add, subtract, multiply};
  14. ptr[2](4,5); //20
  15. }
英文:

You need to initialize during declaration.

  1. //array of function pointers
  2. #include&lt;stdio.h&gt;
  3. void add(int a, int b){
  4. printf(&quot;%d\n&quot;, a+b);
  5. }
  6. void subtract(int a, int b){
  7. printf(&quot;%d\n&quot;, a-b);
  8. }
  9. void multiply(int a, int b){
  10. printf(&quot;%d\n&quot;, a*b);
  11. }
  12. int main(){
  13. void (*ptr[3])(int, int) = {add, subtract, multiply};
  14. ptr[2](4,5); //20
  15. }

huangapple
  • 本文由 发表于 2023年1月6日 15:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定