Reputation: 3184
Is there any macro which will remove double quotes and '\0' in a string?
For example:- "HELLO\0" -> HELLO
Edited:- Actually I want to get the function address. So I thought to use FUNC Macro to get the function name and stipping out double quotes and '\0' will help me to get the function address.
for ex:
#define FUN_PTR fun
#define FUN_NAME "fun"
void fun(){
printf("fun add %p name %s\n",FUN_PTR,FUN_NAME);
}
To avoid user define macros. I like to know other methods to derive these functionality :).
void fun(){
printf("fun add %p name %s\n",<SOME_MACRO>(__FUNC__),__FUNC__);
}
Upvotes: 1
Views: 4245
Reputation: 213298
Do it the other way around.
#define PRINT_PTR(p) printf(#p " == %p\n", (void *) p)
int main()
{
PRINT_PTR(exit);
return 0;
}
Inside a macro, if x
is a macro argument, then #x
is the string version.
#define PRINT_INT(i) printf(#i " == %d\n", i)
PRINT_INT(5 + ~3);
PRINT_INT(atoi("1234"));
Typically, if you use this a lot, then you'll want to define a helper function to work around the weak type system in C:
void print_ptr_func(char const *s, int v);
#define PRINT_PTR(p) print_ptr_func(#p, p)
Note that this won't work for getting the address of the current function. __FUNC__
is not a macro and it is not a string literal, it has no double quotes. You cannot use __FUNC__
to get the address of the function without some serious trickery, and it will break half the time. For example:
#define PRINT_FUNC_INFO() print_func_info(__FUNC__)
#include <dlfcn.h>
void print_func_info(char const *n)
{
void *p = dlsym(RTLD_DEFAULT, n);
printf("Function %s, address %p\n", n, p);
}
However, this will not work half the time -- dlsym
wasn't designed to be used for that purpose.
Upvotes: 4