C编程- void(*ptr[2]) = {blah, blah2};解释

时间:2021-05-28 18:54:42

Can someone explain what is going on here in this question?

有人能解释一下这个问题是怎么回事吗?

Print 1 to 100 with out loop and conditions in C

打印1到100,输出循环和C中的条件

What is the theory or possible low-level explanation to

理论或可能的低层次解释是什么

void(*ptr[2])() = {blah, blah2};

空白(* ptr[2])()= {胡说,blah2 };

I'm new to C, and I would like to know what is going on here ;)

我刚到C,我想知道这是怎么回事。

Thank's in advance.

提前感谢的。

P.S: I searched around a bit but unfortunately couldn't find anything.

P。S:我找了一会儿,但不幸的是什么也没找到。

6 个解决方案

#1


7  

Do you mean how do I read this?

你是说我怎么看?

You discarded some useful context from the original answer, so let's repair that:

你从原来的答案中删除了一些有用的上下文,所以让我们修复一下:

void print();
void exitme();

int main() {
    void (*p[2])()={print,exitme};

so now it's obvious that print and exitme refer to functions. Specifically, they both return void and take no arguments, so they share the function pointer type

显然,print和exitme是指函数。具体来说,它们都返回void并没有参数,所以它们共享函数指针类型。

void (*)()

(that is, a function pointer of that type can point to either print or exitme). For example,

(也就是说,该类型的函数指针可以指向print或exitme)。例如,

void (*print_or_exit)() = (rand() % 2) ? print : exitme;
print_or_exit(); // who knows what it will do!

Now, an array of two of these function pointers looks like:

现在,这些函数指针中的两个数组如下:

void (*ptr[2])()

and can be initialized as:

可初始化为:

void (*ptr[2])() = { print, exitme };

so now we can rewrite our stupid example as:

现在我们可以把这个愚蠢的例子重写为

void (*print_or_exit)() = ptr[rand() % 2];

#2


7  

It's just an array of function pointers with two elements - ptr[0] points to the function blah() and ptr[1] points to the function blah2().

它只是一个包含两个元素的函数指针的数组——ptr[0]指向函数blah()而ptr[1]指向函数blah2()。

You can call each function like this:

你可以这样调用每个函数:

(ptr[0])(); // call blah()

(ptr[1])(); // call blah2()

#3


2  

That is declaring ptr as an array of two function pointers, and initializing them to point to the two functions blah() and blah2(), respectively.

它将ptr声明为两个函数指针的数组,并将它们初始化为指向两个函数blah()和blah2()。

The parenthesis are needed to protect it from becoming a declaration of void * (void pointers), which would be something else.

需要括号来保护它不成为void * (void指针)的声明,这是其他东西。

#4


2  

This is an array of function-pointers. You are initializing it with the addresses of the two functions blah and blah2.

这是一个函数指针数组。你用两个函数的地址来初始化它。

#5


2  

void(*ptr[2])()

Declares an array of function pointers, where blah and blah2 are the functions that are pointed to. In this case the functions that are pointed to must have void as 'return type' (ie they must not return anything) and must not have any parameters (due to the () at the end of the declaration).

声明一个函数指针数组,其中blah和blah2是指向的函数。在这种情况下,指向的函数必须有void作为“返回类型”(即它们不能返回任何东西),并且不能有任何参数(因为在声明的末尾有())。

The example code you link to, uses these function pointers to either print the value of i or exit the application (when i = 100 and i/100 result in 1, which is the second array element)

您所链接的示例代码使用这些函数指针来打印i的值或退出应用程序(当i = 100和i/100时,结果是1,这是第二个数组元素)

As the example shows you can call a function using the function liek this:

如示例所示,您可以使用函数liek调用一个函数:

i = 0; // or 1 (as your array has a size of 2)
(ptr[i])();

Note: the example could be considered (very) bad practice, so please do not use it instead of a simple for-loop! :)

注意:这个示例可能被认为是(非常)糟糕的实践,所以请不要使用它,而要使用一个简单的for-loop!:)

#6


1  

ptr is an array of pointers to a function that doesn't return any value and has no arguments.

ptr是一个指向函数的数组,该函数不返回任何值,也没有参数。

in general, ptr[1] is calling to exitme function, which calls exit(1) and ptr[0] calls to a function that handles the printing. the variable i is static so its usage ends when the program quits.

通常,ptr[1]调用exitme函数,该函数调用exit(1)和ptr[0]调用处理打印的函数。变量i是静态的,所以当程序退出时,它的使用就结束了。

at the first iteration, there is a new memory block allocated for array of two pointers that points to exitme and print functions (actually at each iteration, this is done, not like with the i). then, a static variable i is declared and initialized to zero (for sure in ansi-c), later the first function called (print) because i++/100 is 0.001 but it's equal to 0 (because neither a cast has been done nor they are float) and on the same time i is being increased. after calling print and printing the value of i, we call again to main and this keeps iterating, til i gets to 100.

在第一个迭代中,有一个新的为数组分配内存块的两个指针指向exitme和打印功能(实际上在每个迭代中,这样做,不像我)。然后,我声明一个静态变量初始化为0(当然在ansi c),之后第一个函数称为(印刷)因为我+ + / 100是0.001但是它等于0(因为演员已经完成和浮动)和在同一时间我正在增加。在调用print并打印i的值之后,我们再次调用main,这就不断地迭代,直到到达100。

note that there are two static integers i but each one has different scope, therefore, they have different values.

注意有两个静态整数i,但是每个都有不同的范围,因此,它们有不同的值。

thus you print 1...100 without using loop.

因此你打印1…100年不使用循环。

#1


7  

Do you mean how do I read this?

你是说我怎么看?

You discarded some useful context from the original answer, so let's repair that:

你从原来的答案中删除了一些有用的上下文,所以让我们修复一下:

void print();
void exitme();

int main() {
    void (*p[2])()={print,exitme};

so now it's obvious that print and exitme refer to functions. Specifically, they both return void and take no arguments, so they share the function pointer type

显然,print和exitme是指函数。具体来说,它们都返回void并没有参数,所以它们共享函数指针类型。

void (*)()

(that is, a function pointer of that type can point to either print or exitme). For example,

(也就是说,该类型的函数指针可以指向print或exitme)。例如,

void (*print_or_exit)() = (rand() % 2) ? print : exitme;
print_or_exit(); // who knows what it will do!

Now, an array of two of these function pointers looks like:

现在,这些函数指针中的两个数组如下:

void (*ptr[2])()

and can be initialized as:

可初始化为:

void (*ptr[2])() = { print, exitme };

so now we can rewrite our stupid example as:

现在我们可以把这个愚蠢的例子重写为

void (*print_or_exit)() = ptr[rand() % 2];

#2


7  

It's just an array of function pointers with two elements - ptr[0] points to the function blah() and ptr[1] points to the function blah2().

它只是一个包含两个元素的函数指针的数组——ptr[0]指向函数blah()而ptr[1]指向函数blah2()。

You can call each function like this:

你可以这样调用每个函数:

(ptr[0])(); // call blah()

(ptr[1])(); // call blah2()

#3


2  

That is declaring ptr as an array of two function pointers, and initializing them to point to the two functions blah() and blah2(), respectively.

它将ptr声明为两个函数指针的数组,并将它们初始化为指向两个函数blah()和blah2()。

The parenthesis are needed to protect it from becoming a declaration of void * (void pointers), which would be something else.

需要括号来保护它不成为void * (void指针)的声明,这是其他东西。

#4


2  

This is an array of function-pointers. You are initializing it with the addresses of the two functions blah and blah2.

这是一个函数指针数组。你用两个函数的地址来初始化它。

#5


2  

void(*ptr[2])()

Declares an array of function pointers, where blah and blah2 are the functions that are pointed to. In this case the functions that are pointed to must have void as 'return type' (ie they must not return anything) and must not have any parameters (due to the () at the end of the declaration).

声明一个函数指针数组,其中blah和blah2是指向的函数。在这种情况下,指向的函数必须有void作为“返回类型”(即它们不能返回任何东西),并且不能有任何参数(因为在声明的末尾有())。

The example code you link to, uses these function pointers to either print the value of i or exit the application (when i = 100 and i/100 result in 1, which is the second array element)

您所链接的示例代码使用这些函数指针来打印i的值或退出应用程序(当i = 100和i/100时,结果是1,这是第二个数组元素)

As the example shows you can call a function using the function liek this:

如示例所示,您可以使用函数liek调用一个函数:

i = 0; // or 1 (as your array has a size of 2)
(ptr[i])();

Note: the example could be considered (very) bad practice, so please do not use it instead of a simple for-loop! :)

注意:这个示例可能被认为是(非常)糟糕的实践,所以请不要使用它,而要使用一个简单的for-loop!:)

#6


1  

ptr is an array of pointers to a function that doesn't return any value and has no arguments.

ptr是一个指向函数的数组,该函数不返回任何值,也没有参数。

in general, ptr[1] is calling to exitme function, which calls exit(1) and ptr[0] calls to a function that handles the printing. the variable i is static so its usage ends when the program quits.

通常,ptr[1]调用exitme函数,该函数调用exit(1)和ptr[0]调用处理打印的函数。变量i是静态的,所以当程序退出时,它的使用就结束了。

at the first iteration, there is a new memory block allocated for array of two pointers that points to exitme and print functions (actually at each iteration, this is done, not like with the i). then, a static variable i is declared and initialized to zero (for sure in ansi-c), later the first function called (print) because i++/100 is 0.001 but it's equal to 0 (because neither a cast has been done nor they are float) and on the same time i is being increased. after calling print and printing the value of i, we call again to main and this keeps iterating, til i gets to 100.

在第一个迭代中,有一个新的为数组分配内存块的两个指针指向exitme和打印功能(实际上在每个迭代中,这样做,不像我)。然后,我声明一个静态变量初始化为0(当然在ansi c),之后第一个函数称为(印刷)因为我+ + / 100是0.001但是它等于0(因为演员已经完成和浮动)和在同一时间我正在增加。在调用print并打印i的值之后,我们再次调用main,这就不断地迭代,直到到达100。

note that there are two static integers i but each one has different scope, therefore, they have different values.

注意有两个静态整数i,但是每个都有不同的范围,因此,它们有不同的值。

thus you print 1...100 without using loop.

因此你打印1…100年不使用循环。