C Unix/Linux programming: Which type is md_addr_t?
GCC
gives me the following:
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
. How can I cast this to char*
and what does md_addr_t
meant to be? (Neither man-page could help me nor 开发者_开发技巧google)
The reason has been pointed out by larsmans correctly. This warning is seen when casting const
away i.e. if a function take non-const
argument but you pass const
argument, then if the function modifies the argument passed you have undefined behaviour. md_addr_t
which is typedef in the code has nothing to do with this. 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):
#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. (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[]
. 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. As__FILE__
is also constant you can consider changing to _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. 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...
精彩评论