473,722 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Warning when comparing char[] to a #define'd string

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

Nov 7 '08 #1
13 2723
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
Nov 7 '08 #2

"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

Nov 7 '08 #3
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.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 7 '08 #4
Andreas Eibach wrote, On 07/11/08 19:10:
"Stephen Sprunk" <st*****@sprunk .orgwrote:
<snip>
/* ... get a couple of bytes ( = string) from buffer ... */
if (strcmp (BLAH, yadda) == 0) { printf ("yeehaw!\n" ); }

gives me even TWO warnings!
See my other reply.
So I will stick to using _signed_ char* in the strcmp(), even though I'm not
needing negative values.
Plain char is NOT signed char. On some systems it happens to be signed,
on some it is unsigned, but it is ALWAYS a distinct type from both.

Always use plain char for character data *unless* there is a specific
good reason to use something else.
Looks like strcmp() does not accept unsigned char*, even though both
arguments are now of same type.
<snip>

See my other reply.
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 7 '08 #5
Andreas Eibach wrote:
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
Are you sure the error message matches the code you have shown?
If so, report the bug[*] to your compiler vendor. (Or upgrade to a
newer version. For what it's worth, your code elicits a warning from
gcc 4.3.2, but with that version the warning is correct.)
[*] Technically speaking it isn't a bug, because a compiler is
allowed to issue any diagnostic messages it cares to, even if those
messages are incorrect. Nonetheless, I think the maintainers of
the compiler in question would consider this behavior a bug, and
would thank you for reporting it.

--
Er*********@sun .com
Nov 7 '08 #6
Andreas Eibach wrote:
>
.... snip ...
>
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.
No. You should recognize that there are three distinct types of
chars in C. char, signed char, and unsigned char. They are
distinct. For any implemented C system char has been set to be one
of signed char, or unsigned char. You don't necessarily know
which. What you do know is that strings are represented by arrays
of char (with a '\0' terminator), and are passed as pointers to
char (char*).

It looks as if your system has char set to be signed char. Don't
assume this in general.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Nov 8 '08 #7
Andreas Eibach wrote:
"Stephen Sprunk" <st*****@sprunk .orgwrote:
>>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!
They work because they're correct.
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!
What warnings? I presume you mean the original warning about comparing
strings with different signed-ness is gone and there are two new ones
about passing (unsigned char *)s to a function expecting (char *)s?

If I saw this code, I'd warn you, too. (unsigned char *) is not the
right type to be using with strings. It'll probably work, but it
misleads the reader as to what's going on.
So I will stick to using _signed_ char* in the strcmp(), even though I'm not
needing negative values.
No, do not use (signed char *). When working with strings, always use
(char *). Even if (char) happens to be signed on your system, it is
still a distinct type from (signed char).
Looks like strcmp() does not accept unsigned char*, even though both
arguments are now of same type.
strcmp() is specified to take two arguments of type (char *). If you
pass in (signed char *) _or_ (unsigned char *), it's not surprising that
your compiler warns you about using the wrong type. When working with
strings, always use (char *).

S
Nov 8 '08 #8

"Flash Gordon" <sm**@spam.caus eway.comschrieb :
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.
True. I was just trying to keep it short :) but I also did try this 4th
variation.
And of course, I got 2 warnings about non-respected signedness.
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.

Yes, I agree I had confused and set this equal to (un)signed int.
So there are three of the char* sort. I think now I won't forget about that
anymore. You definitely never stop learning. o_o

-Andreas

Nov 8 '08 #9

"Eric Sosman" <Er*********@su n.comwrote:
Are you sure the error message matches the code you have shown?
If so, report the bug[*] to your compiler vendor. (Or upgrade to a
newer version. For what it's worth, your code elicits a warning from
gcc 4.3.2,
Bingo. I was getting this with 4.3.2 indeed.
but with that version the warning is correct.)
Yup. Note that my post definitely was _not_ a complaint of that "gcc guys,
please fix your software" sort, but simply a presumption that I might have
forgotten about something. :) However, I could not sense that there are
three distinct char * types, unlike the int types which are either unsigned
or signed---thus two---and that's about it.

-Andreas

Nov 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6443
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the point of declaration of this variable, correct?): static const char * g_application_name = "Tiny, class-based MDI Example"; In another source file, I'm including the header globals.hpp and I'm using the variable g_application_name. I do,...
27
2343
by: ruel loehr | last post by:
Hey guys, I am looking for some insight: Given two sorted arrays of integers A and B, where array B has enough extra room in it to hold the contents of both A and B. Merge array A and B together such that the result is the sorted combination of the two (do not remove duplicates) and the result resides in the B array. I cannot perform any memory allocations to solve the problem.
43
2724
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It holds strings of any length. I guess what is happening here is that this array initially holds only '\0' and hence is of length 1. But sometimes, when I tried to write some functions and do some
3
2724
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released storage Keywords.Keyword reachable from global A global variable does not satisfy its annotations when control is transferred. (Use -globstate to inhibit warning) keywords.c:60:11: Storage Keywords.Keyword released
18
4741
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
5
16170
by: Roman Mashak | last post by:
Hello, All! I already posted my question and received valuable feedbacks, I changed my code as was proposed here but still receive the same error of valgrind. SO, the code is: #define DB_IS_FLD_EMPTY(x) (((x) && *(x)) ? 0 : 1) int db_is_fld_empty(char *fld) { return DB_IS_FLD_EMPTY(fld);
2
2754
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
5
11223
by: J.Broeden | last post by:
Hi, I've written a some code to assist in my understanding of strings using some of Cs built in character handling functions but I am not sure why I'm getting the following error. I hope someone can explain the errors of my ways. /* Looking for special chars in testlist */ #include <stdio.h> #include <string.h> #include <ctype.h>
22
1827
by: mdh | last post by:
Hi All, Happy Solstice! May I ask the following. The following is a brief excerpt of a practice program. main(...){ if (argc 1 && mystrcomp(argv, "-n") == 0) /* argv is "-n" / ******/ .......}
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.