#include <stdio.h>
#define slice(bare_string,start_index) #bare_string+start_index
#define arcane_slice(bare_string,start_index) "ARCANE" #bare_string+start_index
int main(){
printf("slice(FIRSTA,0)==> `%s`\n",slice(FIRSTA,0));
printf("slice(SECOND,2)==> `%s`\n",slice(SECOND,2));
printf("slice(THIRDA,5)==> `%s`\n",slice(THIRDA,5));
printf("slice(FOURTH,6)==> `%s`\n",slice(FOURTH,6));
printf("slice(FIFTHA,7)==> `%s`\n",slice(FIFTHA,7));
printf("arcane_slice(FIRSTA,0)==> `%s`\n",arcane_slice(FIRST,0));
printf("arcane_slice(SECOND,2)==> `%s`\n",arcane_slice(SECOND,2));
printf("arcane_slice(THIRDA,5)==> `%s`\n",arcane_slice(THIRDA,5));
printf("arcane_slice(FOURTH,6)==> `%s`\n",arcane_slice(FOURTH,6));
printf("arcane_slice(FIFTHA,7)==> `%s`\n",arcane_slice(FIFTHA,7));
return 0;
}
输出:
slice(FIRSTA,0)==> `FIRSTA`
slice(SECOND,2)==> `COND`
slice(THIRDA,5)==> `A`
slice(FOURTH,6)==> ``
slice(FIFTHA,7)==> `slice(FIFTHA,7)==> `%s`
`
arcane_slice(FIRSTA,0)==> `ARCANEFIRST`
arcane_slice(SECOND,2)==> `CANESECOND`
arcane_slice(THIRDA,5)==> `ETHIRDA`
arcane_slice(FOURTH,6)==> `FOURTH`
arcane_slice(FIFTHA,7)==> `IFTHA`
我有上面需要帮助的 C 代码。我从应该从传递的索引“切片”
到字符串末尾的
类似函数的宏中得到奇怪的行为。它不是真正意义上的切片,而是
从某个点传递一个指针,从该
地址开始打印。我设法弄清楚在字符串
中首先连接然后“切片”。我还发现当
等于 6时从空字节开始打印,这就是
你得到“空”字符串的原因。奇怪的部分是什么时候是 7。它将第一个参数打印
到(interpolator string) 并与两者中传递的裸字符串连接。slice
printf
arcane_slice
start_index
printf
start_index
printf
arcane_slice
和slice
(如输出中的第 5 行和第 10 行所示)
为什么会这样?
我最疯狂的猜测是,当start_index
超过字符串的长度时,
指针指向程序地址空间中数据段的开头。但是
你可以用“为什么它不开始打印FIRSTA
” 来反驳它