Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to extract name of C variable from C declaration.

Status
Not open for further replies.

judgex

Programmer
Jun 11, 2004
1
AU
Hi everyone,

I only want the name of the declared variable. Everything else is not important to me:

char x[50]; //ANS:x
int y; //ANS:y
char z; //ANS:z
int (*f)(double); //ANS:y
union un u; //ANS:u
struct x y; //ANS:y
struct w w[25]; //ANS:w
struct z z; //ANS:z
double c; //ANS:c

Can somebody give me some shell or C code which extracts the name of a C variable from an arbitrary input? The above are only examples.

Thanks.
 
You probably have to do it as a macro
Code:
#define DISP(name,fmt) printf(#name "=" fmt, name)

DISP ("%s", x);
DISP ("%d", y);
DISP ("a structure", y);
 
Why exactly would you need to extract variable names anyways?
 
> from an arbitrary input
Are we talking about the single lines you posted, or is this heading to the kind of input which is in fact a C program.

The 'cdecl' program (for which source is available) might be useful for parsing single line declarations like that. There's even a primitive version listed in K&R


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top