我有这样的程序,
void test(char* a)
{
printf("%s",a); // Trying to print the function name "add" here
return;
}
int add(int,int)
{
test(__FUNCTION__); // I need the function name add to be passed to the test function
...................
....................
}
Run Code Online (Sandbox Code Playgroud)
但是在构建时,我在这样的C编译器(gcc风格)中得到了错误,
传递'test'的参数1会丢弃指针目标类型的限定符
请看看这个,
/ R
从这里:
标识符
__func__由翻译器隐式声明,就像紧跟在每个函数定义的左括号之后的声明一样Run Code Online (Sandbox Code Playgroud)static const char __func__[] = "function-name";出现了,其中function-name是词法封闭函数的名称.此名称是函数的简单名称.
__FUNCTION__是另一个名字__func__.
所以,你试图将a传递const char *给a char *,丢弃const限定符.因为在您的test函数中您没有修改传递的字符串,所以将其参数类型更改为const char *,向调用者承诺您不修改其字符串.
顺便说一下,为了获得const正确性,你应该总是记得将指针声明为const"仅输入"参数.