将char []与#define'd字符串进行比较时发出警告 [英] Warning when comparing char[] to a #define'd string

查看:119
本文介绍了将char []与#define'd字符串进行比较时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




让'我说我有这个:


#include< string.h>


#define BLAH" foo"


稍后,我这样做:


unsigned char yadda [ 10];


/ * ...从缓冲区获取几个字节(=字符串)... * /


if( strcmp(BLAH,yadda)== 0){printf(" yeehaw!\ n"); }


这给了我以下警告:

警告:'strcmp'中传递参数1的指针目标在
$ b中有所不同$ b签名

我在问,因为我不知道_quoted_

#define'的签名或未签名。

另外几次尝试投掷这些东西(arg#1 + arg#2)都失败了。

似乎是一个gcc 4-only默认来检查这个。

(我打赌)这也在gcc3中,默认情况下没有启用)


谢谢,

安德烈亚斯

解决方案

Andreas Eibach写道:


让'我说我有这个:


#include< string.h>


#define BLAH" foo"


稍后,我这样做:


unsigned char yadda [10];


/ * ...从缓冲区获取几个字节(=字符串)... * /


if(strcmp(BLAH,yad da)== 0){printf(" yeehaw!\ n"); }



这与以下内容没有区别:


#include< string.h>

unsigned char yadda [10];


/ * ...从缓冲区获取几个字节(=字符串)... * /


if(strcmp(" foo",yadda)== 0){printf(" yeehaw!\ n"); }


这给了我以下警告:

警告:'strcmp'的参数1中的指针目标在
signedness



您正在比较(char *)和(unsigned char *)。这个警告让人觉得很有意思。如果yadda包含一个字符串,它应该是char [],而不是

unsigned char []。


我要问因为我不知道_quoted_

#define'的签名或未签名。



#define与此无关。请参阅上面的简化。


另外几次尝试投射这些东西(arg#1 + arg#2)都失败了。



Casting" foo" to(unsigned char *)或者将yadda转换为(char *)应该

掩盖警告,但更好的解决方案是使用正确的类型。


似乎是一个gcc 4-only默认来检查这个。

(我打赌这也是gcc3中的,默认情况下它没有启用)


可能;请问GCC人员。


char可能在您的系统上未签名,但即使如此,char也是如此。和unsigned

char并不完全一样。它不像int和签署

int是一样的。


S




" Stephen Sprunk" < st ***** @sprunk.orgwrote:


您正在比较(char *)和(unsigned char *)。这个警告让人觉得很有意思。如果yadda包含一个字符串,它应该是char [],而不是

unsigned char []。


我要问因为我不知道_quoted_

#define'的签名或未签名。



#define与此无关。请参阅上面的简化。



OK。


还有几次尝试投掷东西( arg#1 + arg#2都失败了。



Casting" foo" to(unsigned char *)或者将yadda转换为(char *)应该

掩盖警告,但更好的解决方案是使用正确的类型。



感谢您的提示 - 我使用修改后的代码进行了一些测试。

测试了三种变体,其中两种有效,其中一种后者需要一个

额外的malloc()。


现在它来了::))


[变异#1 +#2]


#include< string.h>


#define BLAH" foo"

char * yadda; / *变种#2:char yadda [10]; * /


.... malloc()... / *变种#2:不要使用* /


/ * ...从缓冲区中获取几个字节(=字符串)... * /


if(strcmp(BLAH,yadda)== 0){printf(" yeehaw! \\\
"); }


都工作!


但是


[变种#3]

#include< string.h>


#define BLAH(unsigned char *)" foo"

....

unsigned char yadda [10]; / * unsigned char * ^ = unsigned char [] * /


/ * ...从缓冲区获取几个字节(=字符串)... * /

if(strcmp(BLAH,yadda)== 0){printf(" yeehaw!\ n"); }


给了我两个警告!


所以我会坚持在strcmp()中使用_signed_ char *,即使我' '不是

需要负值。

看起来strcmp()不接受unsigned char *,即使两个

参数现在都是相同的类型。


似乎是一个gcc 4-only默认值来检查这个。

(我打赌这也是在gcc3中,默认情况下它没有启用)



可能;问海湾合作委员会的人。



可能是个好主意。

感谢您的回复,


-Andreas


Stephen Sprunk写道,2008年11月11日17:52:


Andreas Eibach写道:


>让'我说我有这个:

#include< string.h>

#define BLAH" foo"

稍​​后,我这样做:

unsigned char yadda [10];

/ * ...从缓冲区获取几个字节(=字符串)... * /

if(strcmp(BLAH,yadda)== 0){printf(" yeehaw!\ n" ); }



< snip>


> ;这给了我以下警告:
警告:传递参数1''strcmp''中的指针目标不同
signness



你''比较一个(char *)和一个(unsigned char *)。这个警告让人觉得很有意思。如果yadda包含一个字符串,它应该是char [],而不是

unsigned char []。



不,他不是,他将两者都传递给一个函数,该函数需要指向

到char(不是未签名或签名但是碰巧做了比较。


>我在问,因为我不知道签名或未签名_quoted_
#define'的。



#define与此无关。请参阅上面的简化。


>还有几次尝试投射这些东西(arg#1 + arg#2)都失败了。



Casting" foo" to(unsigned char *)或者将yadda转换为(char *)应该

掩盖警告,但更好的解决方案是使用正确的类型。



投射字符串文字会让事情变得更糟,因为那时它将是b / b
将是签名而不是匹配的两个指针只有一个。


>似乎是一个gcc 4-only默认来检查这个。
(我打赌这个还有gcc3,默认情况下没有启用)



可能;问海湾合作委员会的人。



默认情况下我的机器上没有启用gcc 4.3.2。它启用

如果我指定-ansi -pedantic,这是正确的,因为需要诊断。


char可能是未签名的系统,但即便如此,char也是如此。和unsigned

char并不完全一样。它不像int和签署

int是一样的。



将yadda转换为char *会删除我机器上的警告,但我会认真地考虑它是​​否应该是纯粹的char。

-

Flash Gordon

如果发送垃圾信息我将其发送至 sm**@spam.causeway.com

如果给我发电子邮件,请使用我的回复地址

请参阅我所主办的comp.lang.c Wiki http://clc-wiki.net/


Hi,

let''s say I have this:

#include <string.h>

#define BLAH "foo"

Later on, I do this:

unsigned char yadda [10];

/* ... get a couple of bytes ( = string) from buffer ... */

if (strcmp (BLAH, yadda) == 0) { printf ("yeehaw!\n"); }

This gives me the following warning:
warning: pointer targets in passing argument 1 of ''strcmp'' differ in
signedness
I''m asking because I do not know about signed or unsigned in _quoted_
#define''s.
Also several attempts of casting the stuff (both arg#1 + arg#2) failed.
Seems to be a gcc 4-only default to check this.
(I bet this was there in gcc3 also, it was just not enabled by default)

Thanks,
Andreas

解决方案

Andreas Eibach wrote:

let''s say I have this:

#include <string.h>

#define BLAH "foo"

Later on, I do this:

unsigned char yadda [10];

/* ... get a couple of bytes ( = string) from buffer ... */

if (strcmp (BLAH, yadda) == 0) { printf ("yeehaw!\n"); }

This is no different from:

#include <string.h>

unsigned char yadda [10];

/* ... get a couple of bytes ( = string) from buffer ... */

if (strcmp ("foo", yadda) == 0) { printf ("yeehaw!\n"); }

This gives me the following warning:
warning: pointer targets in passing argument 1 of ''strcmp'' differ in
signedness

You''re comparing a (char *) and an (unsigned char *). The warning makes
sense. If yadda contains a string, it should be a char[], not an
unsigned char[].

I''m asking because I do not know about signed or unsigned in _quoted_
#define''s.

The #define is irrelevant. See the simplification above.

Also several attempts of casting the stuff (both arg#1 + arg#2) failed.

Casting "foo" to (unsigned char *) or casting yadda to (char *) should
mask the warning, but the better solution is to use the correct types.

Seems to be a gcc 4-only default to check this.
(I bet this was there in gcc3 also, it was just not enabled by default)

Possibly; ask the GCC folks.

char might be unsigned on your system, but even so "char" and "unsigned
char" aren''t exactly the same thing. It''s not like "int" and "signed
int" being the same.

S



"Stephen Sprunk" <st*****@sprunk.orgwrote:

You''re comparing a (char *) and an (unsigned char *). The warning makes
sense. If yadda contains a string, it should be a char[], not an
unsigned char[].

I''m asking because I do not know about signed or unsigned in _quoted_
#define''s.


The #define is irrelevant. See the simplification above.

OK.

Also several attempts of casting the stuff (both arg#1 + arg#2) failed.


Casting "foo" to (unsigned char *) or casting yadda to (char *) should
mask the warning, but the better solution is to use the correct types.

Thanks for the tip - I did a few tests with modified code.
Three variations tested, two of them work, with one of the latter needing an
additional malloc().

Now it comes : :))

[variation #1+#2]

#include <string.h>

#define BLAH "foo"
char * yadda; /* variation #2: char yadda[10]; */

....malloc ()... /* for variation #2: do not use */

/* ... get a couple of bytes ( = string) from buffer ...*/

if (strcmp (BLAH, yadda) == 0) { printf ("yeehaw!\n"); }

both WORK!

BUT

[variation #3]
#include <string.h>

#define BLAH (unsigned char*) "foo"
....
unsigned char yadda[10]; /* unsigned char* ^= unsigned char[] */

/* ... get a couple of bytes ( = string) from buffer ... */
if (strcmp (BLAH, yadda) == 0) { printf ("yeehaw!\n"); }

gives me even TWO warnings!

So I will stick to using _signed_ char* in the strcmp(), even though I''m not
needing negative values.
Looks like strcmp() does not accept unsigned char*, even though both
arguments are now of same type.

Seems to be a gcc 4-only default to check this.
(I bet this was there in gcc3 also, it was just not enabled by default)


Possibly; ask the GCC folks.

Could be a good idea.
Thanks for the reply,

-Andreas


Stephen Sprunk wrote, On 07/11/08 17:52:

Andreas Eibach wrote:

>let''s say I have this:

#include <string.h>

#define BLAH "foo"

Later on, I do this:

unsigned char yadda [10];

/* ... get a couple of bytes ( = string) from buffer ... */

if (strcmp (BLAH, yadda) == 0) { printf ("yeehaw!\n"); }

<snip>

>This gives me the following warning:
warning: pointer targets in passing argument 1 of ''strcmp'' differ in
signedness


You''re comparing a (char *) and an (unsigned char *). The warning makes
sense. If yadda contains a string, it should be a char[], not an
unsigned char[].

No he isn''t, he is passing both to a function that expects to pointers
to char (not unsigned or signed but plain) which happens to do a comparison.

>I''m asking because I do not know about signed or unsigned in _quoted_
#define''s.


The #define is irrelevant. See the simplification above.

>Also several attempts of casting the stuff (both arg#1 + arg#2) failed.


Casting "foo" to (unsigned char *) or casting yadda to (char *) should
mask the warning, but the better solution is to use the correct types.

Casting the string literal will just make matters worse because then it
will be two pointers which don''t match in signedness instead of just one.

>Seems to be a gcc 4-only default to check this.
(I bet this was there in gcc3 also, it was just not enabled by default)


Possibly; ask the GCC folks.

It isn''t enabled by default on my machine with gcc 4.3.2. It is enabled
if I specify -ansi -pedantic, which is correct as a diagnostic is required.

char might be unsigned on your system, but even so "char" and "unsigned
char" aren''t exactly the same thing. It''s not like "int" and "signed
int" being the same.

Casting yadda to char* removes the warning on my machine, but I would
seriously consider whether it should be plain char.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/


这篇关于将char []与#define'd字符串进行比较时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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