sscanf %*s

一次在源码里看到 %*s 的格式,从未见过百思不得其解,今天用google的code搜索,搜到一些使用范例,猜测%*s 是说这里有一些字符,长度不一定,按正则表达式的习惯,*代办任意非负整数。例如:

sscanf(buf, "SPAMD/%18s %d %*s", versbuf, &response)

有3个格式参数 %18s %d %*s,只有两个接收者 versbuf 和 response,按位置匹配,
versbuf 对应 %18s,response 对应 %d,剩下的那个%*s是说这里可能还有一些字符串。
从这个例子看不出 %*s 作为占位符的作用,如果是下面这样:

sscanf(mPos, "#define %*s %d #define %*s %d", &mWidth, &mHeight)

%*s的作用就很明显了,作为一个 Place Holder 他代表了一个字符串,但无需给任何变量赋值,
该字符串后的第一个数字被赋给了mWidth,类似的操作也作用在mHeight上。

以下面的小程序为例:
#include <stdio.h>
int main()
{
char *pSentence = "hi 5 de 6";
int i5, i6;
sscanf(pSentence, "%*s%d%*s%d", &i5, &i6);
printf("%d, %d", i5, i6);
return 0;
}
输出是 5, 6
如果把sscanf(pSentence, "%*s%d%*s%d", &i5, &i6);
改成sscanf(pSentence, "%d %d", &i5, &i6);
输出是什么呢,自己试试吧
原文地址:https://www.cnblogs.com/zhoug2020/p/3459712.html