On Thu, 1 Jan 2004, Servé Lau wrote:
suppose I want to use sscanf get the functionname from a function prototype.
Is the following format string correct then?
char funcname[128];
char *p = "func(void)";
sscanf(p, "%[^(]s", funcname); // "%[^(]s"
First, "%[^(]" will read everything up to the '(' symbol. The 's' after it
is unncessary.
Second, you assume that the function prototype will not indicate the
return value for the function and there will be no extra spaces between
the function name and the '(' symbol.
This second problem will be the hardest part to get around. In some cases
the code might implicitly assume an int return value. In other cases the
return value will be explicitly given. For example,
p = "void srand(unsigned int seed)";
In this case you will get "void srand" as the function name. You could
also have:
p = "unsigned long something(void)";
First thought might be to take the last word of the new string. Even this
does not work since you could have:
p = "void *malloc(size_t size)";
Bottom line, define all your cases before you code a solution.
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to
vi************@whitehouse.gov