今天遇见了一个这样样的写法,来实现goto,感觉很棒,看代码能看出来大概实现,但是为什么能这样实现不太清楚,有没有感兴趣的大佬指点一下迷津。为什么可以这样实现。
我的理解: 对DROP取二级地址放到指针数组里面,元素1号位置是DROP这个字符串的二级地址,然后对元素 * 解引用,就可以输出地址里面的东西就是DROP。
我的疑问:1.这个数组为什么可以这样定义呢?2.为什么printf打印不出来table的元素呢?
#include <stdio.h>
int main ()
{
static const void *table[10] = {
NULL,
&&DROP,
&&ADVANCE,
&&UPF_QER,
&&UPF_URR,
&&UPF_ENCAP_IP4,
&&UPF_ENCAP_IP6,
&&UPF_FOW_FIB,
&&END};
int type;
while(1){
scanf("%d", &type);
goto *(table[type]);
DROP:
printf("type : %d\n", type);
continue;
ADVANCE:
printf("type : %d\n", type);
continue;
UPF_QER:
printf("type : %d\n", type);
continue;
UPF_URR:
printf("type : %d\n", type);
continue;
UPF_ENCAP_IP4:
printf("type : %d\n", type);
continue;
UPF_ENCAP_IP6:
printf("type : %d\n", type);
continue;
UPF_FOW_FIB:
printf("type : %d\n", type);
continue;
END:
printf("type : %d\n", type);
continue;
}
return 0;
}