Servé Lau said:
suppose I want to use sscanf get the functionname from a function prototype.
Is the following format string correct then?
Absolutely not. There are several problems, both logical and syntactical.
char funcname[128];
char *p = "func(void)";
Never assign a string literal to a (non-const) char *. The array
represented by the string literal is non-modifiable. Any attempt to
modify it results in undefined behavior. String literals aren't const
for historical reasons, but you should treat them as if they are.
sscanf(p, "%[^(]s", funcname); // "%[^(]s"
A %[ format specifier without a field width is as bad as gets().
What's with the 's' in the format string? There doesn't seem to be any
way for it to match, since the next character in the stream must be '('.
%[ and %s are completely different format specifiers that happen to have
some similarities.
The '//' stuff at the end of this line is probably a syntax error,
unless you happen to be using a C99 compiler (which is unlikely).
Finally, this approach is far too simplistic to work in general.
Consider these function prototypes:
int f1(void);
int f2 (void);
long int f3(void);
static long unsigned int f4(void);
int (*f5(void))(void);
I don't believe it's possible to build a scanf format string that can
match a function prototype, in general. You need some real parsing logic
for that.
-Kevin