This question already has an answer here:
这个问题在这里已有答案:
- Printing 1 to 1000 without loop or conditionals 106 answers
打印1到1000没有循环或条件106答案
i see the question on a c++ programming context, i check for a solution and one of my friend give me this code its works perfect but i can't understand it's logic and also how it's works. i asked to him about it but he also don't know how the program is actually works, i think he is also take this solution from somewhere. Anybody can explain the logic behind this i mean in the line (&main +
(&exit - &main)*(j/1000))(j+1);
?
我看到关于c ++编程上下文的问题,我检查一个解决方案,我的一个朋友给我这个代码它的工作完美但我无法理解它的逻辑以及它是如何工作的。我向他询问了这件事,但他也不知道该程序是如何运作的,我认为他也是从某个地方采取这个解决方案。任何人都可以在行中解释我的意思背后的逻辑(&main +(&exit - &main)*(j / 1000))(j + 1); ?
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d\n", j);
(&main + (&exit - &main)*(j/1000))(j+1);
}
Thanks in advance
提前致谢
2 个解决方案
#1
28
It works as follows:
它的工作原理如下:
Performs the int
division j/1000
, which will return 0
always while j
is smaller than 1000
. So the pointer operation is as follows:
执行int division j / 1000,当j小于1000时,它将返回0。因此指针操作如下:
&main + 0 = &main, for j < 1000.
Then it calls the resulting function pointed by the pointer operations passing as parameter j+1
. While j
is less than 1000
, it will call main recursively with parameter one more than the step before.
然后它调用由作为参数j + 1传递的指针操作指向的结果函数。当j小于1000时,它将以比前一步更多的参数1递归调用main。
When the value of j
reaches 1000
, then the integer division j/1000
equals to 1
, and the pointer operation results in the following:
当j的值达到1000时,整数除法j / 1000等于1,指针操作产生以下结果:
&main + &exit - &main = &exit.
It then calls the exit
function, which finishes the program execution.
然后它调用exit函数,完成程序执行。
#2
4
I go with the explanation already given but it would be easier to understand if written as below:
我已经给出了已经给出的解释,但如果写成如下,将更容易理解:
void main(int j) {
if(j == 1001)
return;
else
{
printf("%d\n", j);
main(j+1);
}
}
The above code does the same as already written code.
上面的代码与已编写的代码相同。
#1
28
It works as follows:
它的工作原理如下:
Performs the int
division j/1000
, which will return 0
always while j
is smaller than 1000
. So the pointer operation is as follows:
执行int division j / 1000,当j小于1000时,它将返回0。因此指针操作如下:
&main + 0 = &main, for j < 1000.
Then it calls the resulting function pointed by the pointer operations passing as parameter j+1
. While j
is less than 1000
, it will call main recursively with parameter one more than the step before.
然后它调用由作为参数j + 1传递的指针操作指向的结果函数。当j小于1000时,它将以比前一步更多的参数1递归调用main。
When the value of j
reaches 1000
, then the integer division j/1000
equals to 1
, and the pointer operation results in the following:
当j的值达到1000时,整数除法j / 1000等于1,指针操作产生以下结果:
&main + &exit - &main = &exit.
It then calls the exit
function, which finishes the program execution.
然后它调用exit函数,完成程序执行。
#2
4
I go with the explanation already given but it would be easier to understand if written as below:
我已经给出了已经给出的解释,但如果写成如下,将更容易理解:
void main(int j) {
if(j == 1001)
return;
else
{
printf("%d\n", j);
main(j+1);
}
}
The above code does the same as already written code.
上面的代码与已编写的代码相同。