Most_Arduous_Journey
Most_Arduous_Journey

Reputation: 63

Is there a way to get the names of the parameters of a function in C

in python there's inspect.argspec which will give you data about a function including the parameter names, but in C I have no idea how I would get this information from a function. I essentially want to write a function that takes a function pointer and returns an array of strings representing the names of the parameters

Upvotes: 2

Views: 633

Answers (3)

alinsoar
alinsoar

Reputation: 15793

There is no way in the C language to get the names of parameters but it is possible to compile it with the dwarf option and in this case it will keep the names of parameters inside the output executable. For such executable you can get the names of parameters in the same way as gdb does.

You can use the same library as gdb. You should study how to use the dwarf, start reading from 6.1 Symbol Reading.

Upvotes: 4

skyking
skyking

Reputation: 14359

There's no such mechanism available in the language.

If you had done some investigation you would have found that if your program isn't compiled with debugging info then there are no such symbols in the executable anywhere. And consequently the program itself couldn't have access to them during runtime either.

However if you had compiled with debugging symbols (which aren't part of the language) you could possibly write code to retreive the symbols.

Upvotes: 1

Caleb
Caleb

Reputation: 124997

I essentially want to write a function that takes a function pointer and returns an array of strings representing the names of the parameters

Function parameters in C don't have names outside the definition of the function, and the names disappear entirely once the code is compiled. Debuggers can use symbol files generated by the compiler to display the names of functions, parameters, and variables, but in general all that information is stripped out during compilation.

Upvotes: 4

Related Questions