Skip to content

Instantly share code, notes, and snippets.

@macrat
Last active November 26, 2015 12:52
Show Gist options
  • Select an option

  • Save macrat/fdb74c122c8f9e22d225 to your computer and use it in GitHub Desktop.

Select an option

Save macrat/fdb74c122c8f9e22d225 to your computer and use it in GitHub Desktop.
forもwhileもifもswitchも、分岐する文を全て使わずにループを書きたかった。
#include <stdio.h>
void void_func(int v){
}
void loop(int i){
printf("hello, world! %d\n", i);
((void (*)(int))((long long)loop * (i<9) + (long long)void_func * (i>=9)))(i + 1);
}
int main(){
loop(0);
return 0;
}
// 改良してみた版
#include <stdio.h>
void end(int i){
}
void loop(int i){
printf("hello, world! %d\n", i);
(void (*[])(int)){end, loop}[i < 9](i + 1);
}
int main(){
loop(0);
return 0;
}
@macrat

macrat commented Nov 13, 2015

Copy link
Copy Markdown
Author

なるほどな? 綺麗だ。

@ncaq

ncaq commented Nov 26, 2015

Copy link
Copy Markdown

なるほど関数ポインタ配列の変数名書かなくても良いのか…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment