473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

^ format string

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"

Nov 14 '05 #1
6 3368
"Servé Lau" <i@bleat.nospam.com> wrote in message
news:bt**********@news4.tilbu1.nb.home.nl...
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"


Almost, but not quite. For example, *I* write function prototypes like this:

int func (void);

Note the return type (I never rely on implicir int), *at least one* space
between the return type and the function name an *at least one* space
between
the function name and the opening parenthesis.

Many people prefer even more spaces, e.g.:

int func ( void ) ;
Nov 14 '05 #2
On Thu, 1 Jan 2004 19:56:46 +0100, "Servé Lau" <i@bleat.nospam.com>
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"

How do you skip over the function's return type which should be part
of the prototype?
<<Remove the del for email>>
Nov 14 '05 #3
Servé Lau wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #4
"Peter Pichler" <pi*****@pobox.sk> writes:
"Servé Lau" <i@bleat.nospam.com> wrote in message
news:bt**********@news4.tilbu1.nb.home.nl...
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"


Almost, but not quite. For example, *I* write function prototypes like this:

int func (void);

Note the return type (I never rely on implicir int), *at least one* space
between the return type and the function name an *at least one* space
between
the function name and the opening parenthesis.

Many people prefer even more spaces, e.g.:

int func ( void ) ;


Of course there are even more possibilities:

int /* comment */ func ( void );

int fu\
nc(void);

#define NAME func
int NAME(void);

If you want 100% reliability, you'll need a full C parser.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #5
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
Nov 14 '05 #6
da*****@NOMORESPAMcs.utoronto.ca.com (Darrell Grainger) writes:
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.


It's a fairly common convention to put the name in a function
definition at the beginning of a line. It makes searching with
stupid tools easy. If the OP is dealing with code structured
this way, his technique should be okay. (He might still want to
strip white space.)
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
4
by: David Morris | last post by:
Hi Could somebody please explain what the following line of code means String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1 It's actually the first argument that I don't understand. What is...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
7
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.