英文:
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<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}; 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<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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论