[英]C Unix/Linux programming: Which type is md_addr_t?
GCC
gives me the following: GCC
给我以下内容:
warning: passing argument 2 of ‘_panic’ discards qualifiers from pointer target type
misc.h:191: note: expected ‘char *’ but argument is of type ‘const char *’
The second argument is of type md_addr_t
. 第二个参数的类型为
md_addr_t
。 How can I cast this to char*
and what does md_addr_t
meant to be? 我如何将其转换为
char*
以及md_addr_t
是什么意思? (Neither man-page could help me nor google) (手册页都无法帮助我或Google)
The reason has been pointed out by larsmans correctly. 拉尔斯曼人已经正确指出了原因。 This warning is seen when casting
const
away ie if a function take non-const
argument but you pass const
argument, then if the function modifies the argument passed you have undefined behaviour. 强制转换
const
时会看到此警告,即如果函数采用non-const
参数但您传递了const
参数,那么如果函数修改了传递的参数,则您将具有未定义的行为。 md_addr_t
which is typedef in the code has nothing to do with this. 在代码中为typedef的
md_addr_t
与此无关。 In the code you are getting these warning on using panic
which is defined as follows (source from ss-ppc-little.tgz in your link): 在代码中,您收到有关使用
panic
警告,定义如下(来自链接中ss-ppc-little.tgz的信息):
#ifdef __GNUC__
/* declare a panic situation, dumps core */
#define panic(fmt, args...) \
_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## args)
void
_panic(char *file, char *func, int line, char *fmt, ...)
__attribute__ ((noreturn));
#else /* !__GNUC__ */
void
panic(char *fmt, ...);
#endif /* !__GNUC__ */
On Unix/Linux system __GNUC__
is defined thus the first definition is used in which you are pre pending file name ( __FILE__
), function name ( __FUNCTION__
) & line number ( __LINE__
) before printing out the message. 在Unix / Linux系统上,已定义
__GNUC__
,因此使用第一个定义,即在打印消息前先输入待处理的文件名( __FILE__
),函数名( __FUNCTION__
)和行号( __LINE__
)。 (See this link for details).Here as you can see _panic
expects char*
as first & second arguments but arguments being passed are __FILE__
& __FUNCTION__
where __FUNCTION__
is static const char[]
. (有关详细信息,请参
_panic
链接 。)在这里,您可以看到_panic
char*
作为第一个和第二个参数,但是传递的参数是__FILE__
和__FUNCTION__
,其中__FUNCTION__
是static const char[]
。 You can change _panic(char *file, char *func, int line, char *fmt, ...)
to _panic(char *file, const char func[], int line, char *fmt, ...)
or to _panic(char *file, const char *func, int line, char *fmt, ...)
- as the compiler is complaining fix your warning. 您可以将
_panic(char *file, char *func, int line, char *fmt, ...)
更改为_panic(char *file, const char func[], int line, char *fmt, ...)
或_panic(char *file, const char *func, int line, char *fmt, ...)
-编译器在抱怨修正您的警告。 As __FILE__
is also constant you can consider changing to _panic(const char *file, const char *func, int line, char *fmt, ...)
由于
__FILE__
也是常量,因此您可以考虑更改为_panic(const char *file, const char *func, int line, char *fmt, ...)
Hope this helps! 希望这可以帮助!
grep -R md_addr_t /usr/include
does not return anything in a well-populated /usr/include
, nor does it ring a bell. grep -R md_addr_t /usr/include
在良好填充不返回任何/usr/include
,也不门铃。 It must be something in your application/library. 它必须在您的应用程序/库中。
In any case, casting to char *
is just a matter of prefixing (char *)
, which is also one of the few casts that are guaranteed to always work in C, although casting const
away might mean that someone is going to write to read-only memory, which is not guaranteed to work... 在任何情况下,强制转换为
char *
都只是前缀(char *)
,这也是保证始终在C语言中正常工作的少数强制转换之一,尽管强制转换const
可能意味着有人将要编写读写-仅内存,不能保证正常工作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.