我进行了一些研究,以查找sscanf()源代码。但是我找不到我问题的答案。
当我们sscanf()以这种方式使用时:
char str[50] = "5,10,15";
int x;
sscanf(str,"%d,%s",&x,str);
Run Code Online (Sandbox Code Playgroud)
sscanf()是否支持“递归”缓冲区str?
它不会破坏自修改缓冲区。但要使其(尾)递归,您必须读取到字符串的末尾。
代码片段:
char str[]="5,10,15";
int a[10]={0},x = 0;
while (sscanf(str,"%d,%s",a+ x++,str)>1);
Run Code Online (Sandbox Code Playgroud)
读取所有整数。
由于这并不是真正的递归,并且要读取的字符串不会覆盖字符串中的 asciiz,因此我相信这在意义上是“安全的”:只能在家里尝试。