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

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

Do I have to initialize function pointers one by one?

问题

以下是翻译好的部分:

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

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

这会导致:

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

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

// 函数指针数组
#include <stdio.h>

void add(int a, int b){
    printf("%d\n", a+b);
}


void subtract(int a, int b){
    printf("%d\n", a-b);
}


void multiply(int a, int b){
    printf("%d\n", a*b);
}


int main(){

    void (*ptr[3])(int, int);
    
    
    // ptr[3]={add, subtract, multiply};  这种初始化方式不起作用
    
    // 但这种方式可以正常工作
    ptr[0]=add;
    ptr[1]=subtract;
    ptr[2]=multiply;
    
    ptr[2](3,5); // 15
    
}
英文:

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

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

This gives:

> [Error] expected expression before '{' token

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

//array of function pointers

#include&lt;stdio.h&gt;

void add(int a, int b){
	printf(&quot;%d\n&quot;, a+b);
}


void subtract(int a, int b){
	printf(&quot;%d\n&quot;, a-b);
}


void multiply(int a, int b){
	printf(&quot;%d\n&quot;, a*b);
}


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

答案1

得分: 2

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

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

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

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

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

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

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

// 函数指针数组

#include <stdio.h>

void add(int a, int b){
    printf("%d\n", a+b);
}

void subtract(int a, int b){
    printf("%d\n", a-b);
}

void multiply(int a, int b){
    printf("%d\n", a*b);
}

int main(){

    void (*ptr[3])(int, int) = {add, subtract, multiply};

    ptr[2](4,5); //20

}
英文:

You need to initialize during declaration.

//array of function pointers

#include&lt;stdio.h&gt;

void add(int a, int b){
    printf(&quot;%d\n&quot;, a+b);
}


void subtract(int a, int b){
    printf(&quot;%d\n&quot;, a-b);
}


void multiply(int a, int b){
    printf(&quot;%d\n&quot;, a*b);
}


int main(){
    
    void (*ptr[3])(int, int) = {add, subtract, multiply};
    
    ptr[2](4,5); //20
    
}

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:

确定