DCuser
DCuser

Reputation: 993

Is there a way to get name of argument used in the call of a function in c

I am trying in C to get the name of the variable used when the call to the function was made, as below:

func(varA, varB)

I would like to know the names of the arguments (varA, varB)

I am printing the contents of several matrices, and I was wonderign if there is any easy way to distinguish between them without having to actually send the name as a string.

Thank you

Upvotes: 0

Views: 119

Answers (2)

2501
2501

Reputation: 25752

This is not possible without changing the function definition.

You can pass the names manually or use a macro:

#define func( a , b )    func2( a , b , #a , #b )

func2( type a , type b , const char* namea , const char* nameb ){ ...

When preprocessing the code, operator # will transform the variable into a string.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134326

When a C program is compiled and being executed, there is basically no existence of the variable name anymore.

So, no, there is not way you can get an actual argument's (variable) name from the received parameter, as you wanted.

Upvotes: 1

Related Questions