ちょっと奇をてらって
構造体変数に自分自身のポインタを持たせて、ごにょごにょしてみた。
#include <stdio.h> #include <stdlib.h> struct hoge { struct hoge *self; int i; struct hoge *(*print)(struct hoge *); }; struct hoge *p(struct hoge *v) { printf("%d\n", v->i); v->i += 2; return v; } int main() { struct hoge *val; val = malloc(sizeof(struct hoge)); val->self = val; val->i = 3; val->print = p; val->print(val->self)->print(val->self)->print(val->self); // これでも動く不思議 return 0; }
実行すると
$ gcc --version gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -Wall -ansi -o test test.c $ ./test 3 5 7 $
警告も吐きません。GJ