清除编译器警告的最佳方法是什么? [英] Best way of clearing a compiler warning?

查看:59
本文介绍了清除编译器警告的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您好,


请考虑以下代码片段:


fileptr = fopen(param-> ; filename," r");

if(fileptr == NULL)

{

error(" digest.c:digest_file :打开文件,错误);

返回(-2);

}


使用上面的代码,编译器(gcc)给出以下警告:


digest.c:121:警告:传递arg 1的'错误''丢弃限定符来自

指针目标类型。


函数错误声明如下:


void error(char * errmsg,int code);


为什么会给出这个消息,我该如何让它静音?


谢谢


-

Daniel Rudy


电子邮件地址已经过base64编码以减少垃圾邮件

使用b64decode解码电子邮件地址或uudecode -m


为什么极客喜欢电脑:看聊天日期触摸grep make unzip

s旅行视图手指挂载fcsk更多fcsk是喷雾卸载睡眠

解决方案

Daniel Rudy写道:

考虑以下代码片段:

fileptr = fopen(param-> filename," r");
if(fileptr == NULL)
{
错误( digest.c:digest_file:打开文件,错误);
返回(-2);
}

使用上面的代码,编译器(gcc)给出以下警告:

digest.c:121:警告:传递arg 1的'错误''从指针目标类型中丢弃限定符。

函数错误声明如下:

void error(char * errmsg,int code);

为什么会给出这条消息


因为你传递了一个字符串文字,这是const。 'error''是
声明为char *,这意味着它不承诺不修改其参数的内容。如果确实如此,那么糟糕的事情就会发生。

我该如何沉默呢?



如果'错误''真的没有修改`errmsg' ',改变原型和

函数来取代const char *。


如果它确实修改了'errmsg'',你就会有将消息复制到新的

分配的字符串。


S.


关于时间是11/6/2005 4:59 PM,Skarmander声明如下:

Daniel Rudy写道:

请考虑以下代码片段:

fileptr = fopen(param-> filename," r");
if(fileptr == NULL)
{
错误(" digest.c:digest_file :打开文件,错误);
返回(-2);
}
使用上面的代码,编译器(gcc)给出以下警告:

digest.c:121:警告:传递arg 1的'错误''丢弃限定符来自
指针ta rget类型。

函数错误声明如下:

void error(char * errmsg,int code);

为什么它给出这条消息



因为你传递的是字符串文字,即const。 'error''被声明为采用char *,这意味着它不承诺不修改其参数的内容。如果是这样,坏事就会发生。




我明白了。错误不会修改* errmsg的内容,只需使用syslog或fprintf将它打印到stderr。

我怎么让它沉默?



如果'错误''真的没有修改`errmsg'',改变原型和
函数来取一个const char *相反。

如果它确实修改了'errmsg'',你必须将消息复制到新分配的字符串。

S. / blockquote>


啊,谢谢。


-

Daniel Rudy

电子邮件地址已经过base64编码以减少垃圾邮件

解码电子邮件地址使用b64decode或uudecode -m


为什么极客喜欢电脑:看起来聊天日期触摸grep make unzip

strip view finger mount fcsk more fcsk yes spray umount sleep


Skarmander写道:


Daniel Rudy写道:

考虑以下代码片段:

fileptr = fopen(param-> filename," r");
if(fileptr == NULL)
{
错误(" digest.c:digest_file:Open File",errno);
返回(-2);
}

以上代码,编译器(gcc)给出以下警告:

digest.c:121:
警告:传递arg 1的`错误''丢弃来自
指针目标类型的限定符。

函数错误声明如下:

void error(char * errmsg,int code);

为什么会给出这条消息



因为你传递了一个字符串文字,这是const。




一个字符串文字不是const合格。


-

pete



Hello,

Consider the following code fragment:

fileptr = fopen(param->filename, "r");
if (fileptr == NULL)
{
error("digest.c: digest_file: Open File ", errno);
return(-2);
}

With the above code, the compiler (gcc) gives the following warning:

digest.c:121: warning: passing arg 1 of `error'' discards qualifiers from
pointer target type.

The function error is declared as follows:

void error(char *errmsg, int code);

Why does it give this message and how do I silence it?

Thanks

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep

解决方案

Daniel Rudy wrote:

Consider the following code fragment:

fileptr = fopen(param->filename, "r");
if (fileptr == NULL)
{
error("digest.c: digest_file: Open File ", errno);
return(-2);
}

With the above code, the compiler (gcc) gives the following warning:

digest.c:121: warning: passing arg 1 of `error'' discards qualifiers from
pointer target type.

The function error is declared as follows:

void error(char *errmsg, int code);

Why does it give this message
Because you''re passing a string literal, which is const. `error'' is
declared as taking a char*, meaning it does not promise not to modify
the contents of its argument. If it does, Bad Things will happen.
and how do I silence it?


If `error'' really doesn''t modify `errmsg'', change the prototype and the
function to take a const char* instead.

If it does modify `errmsg'', you''ll have to copy the message to a newly
allocated string.

S.


At about the time of 11/6/2005 4:59 PM, Skarmander stated the following:

Daniel Rudy wrote:

Consider the following code fragment:

fileptr = fopen(param->filename, "r");
if (fileptr == NULL)
{
error("digest.c: digest_file: Open File ", errno);
return(-2);
}

With the above code, the compiler (gcc) gives the following warning:

digest.c:121: warning: passing arg 1 of `error'' discards qualifiers from
pointer target type.

The function error is declared as follows:

void error(char *errmsg, int code);

Why does it give this message


Because you''re passing a string literal, which is const. `error'' is
declared as taking a char*, meaning it does not promise not to modify
the contents of its argument. If it does, Bad Things will happen.



I see. error does not modify the contents of *errmsg, just prints it
out using either syslog or fprintf to stderr.

and how do I silence it?



If `error'' really doesn''t modify `errmsg'', change the prototype and the
function to take a const char* instead.

If it does modify `errmsg'', you''ll have to copy the message to a newly
allocated string.

S.



Ah, thanks.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep


Skarmander wrote:


Daniel Rudy wrote:

Consider the following code fragment:

fileptr = fopen(param->filename, "r");
if (fileptr == NULL)
{
error("digest.c: digest_file: Open File ", errno);
return(-2);
}

With the above code, the compiler (gcc) gives the following warning:

digest.c:121:
warning: passing arg 1 of `error'' discards qualifiers from
pointer target type.

The function error is declared as follows:

void error(char *errmsg, int code);

Why does it give this message



Because you''re passing a string literal, which is const.



A string literal isn''t const qualified.

--
pete


这篇关于清除编译器警告的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆