470,586 Members | 1,342 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

how to make these warnings disappear?


When I compile this code:

typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];

ipString[0] = (BYTE) 0xff & (ip >24);
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;

return ipString;
}

for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."

How can I get rid of these four warnings?

Thanks! -- Pete

Apr 8 '07 #1
6 1657

"pete142" <pe*****@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
>
When I compile this code:

typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];

ipString[0] = (BYTE) 0xff & (ip >24);
Make sure type 'unsigned int' on your system is at least
24 bits wide. The language only requires 16.
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;

return ipString;
}

for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."

How can I get rid of these four warnings?
First, make sure there will indeed be no data lost (i.e.
the value will fit into an 'unsigned char'), or that if
it is, it doesn't matter to your program. Then you can
use a cast. E.g.:

ipString[3] = (BYTE) (0xff & ip);

Use casts with caution. They will often inhibit compiler
warnings that can notify you of things you need to know.

-Mike
Apr 8 '07 #2
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"pete142" <pete...@gmail.comwrote in message

news:11**********************@b75g2000hsg.googlegr oups.com...
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];
ipString[0] = (BYTE) 0xff & (ip >24);

Make sure type 'unsigned int' on your system is at least
24 bits wide. The language only requires 16.
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;
return ipString;
}
for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."
How can I get rid of these four warnings?

First, make sure there will indeed be no data lost (i.e.
the value will fit into an 'unsigned char'), or that if
it is, it doesn't matter to your program. Then you can
use a cast. E.g.:

ipString[3] = (BYTE) (0xff & ip);

Use casts with caution. They will often inhibit compiler
warnings that can notify you of things you need to know.

-Mike
Thanks, Mike. I've done all that you suggested. I am using the
strictest warnings level, and should be able to get a clean, no-
warnings compile out of this code /without/ reducing the warnings
level.

So how can I get rid of these warnings, please? That is, how can I
convince the compiler (using standard-C mechanisms) that I don't need
to be warned about these productions?

-- Pete
Apr 8 '07 #3
On 8 Apr 2007 09:43:00 -0700, "pete142" <pe*****@gmail.comwrote in
comp.lang.c:
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"pete142" <pete...@gmail.comwrote in message

news:11**********************@b75g2000hsg.googlegr oups.com...
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];
ipString[0] = (BYTE) 0xff & (ip >24);
Make sure type 'unsigned int' on your system is at least
24 bits wide. The language only requires 16.
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;
return ipString;
}
for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."
How can I get rid of these four warnings?
First, make sure there will indeed be no data lost (i.e.
the value will fit into an 'unsigned char'), or that if
it is, it doesn't matter to your program. Then you can
use a cast. E.g.:

ipString[3] = (BYTE) (0xff & ip);

Use casts with caution. They will often inhibit compiler
warnings that can notify you of things you need to know.

-Mike

Thanks, Mike. I've done all that you suggested. I am using the
strictest warnings level, and should be able to get a clean, no-
warnings compile out of this code /without/ reducing the warnings
level.

So how can I get rid of these warnings, please? That is, how can I
convince the compiler (using standard-C mechanisms) that I don't need
to be warned about these productions?
As Mike pointed out in his post, but did not emphasize, your cast is
in the wrong place:
ipString[0] = (BYTE) 0xff & (ip >24);
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;
Should be:

ipString[0] = (BYTE) (0xff & (ip >24));
ipString[1] = (BYTE) (0xff & (ip >16));
ipString[2] = (BYTE) (0xff & (ip > 8));
ipString[3] = (BYTE) (0xff & ip);

....otherwise you are just casting the constant 0xff to unsigned char,
which is then promoted to unsigned int because 'ip', shifted or not,
has type unsigned int. So the entire result of the right hand side,
as you wrote it, is unsigned int, and the compiler emits the warning.

The extra set of parentheses allows the conversion of the int constant
expression 0xff to unsigned int, performs the bit-wise AND on the
unsigned int result of 'ip', shifted or not, to produce an unsigned
int result. Then that unsigned int is converted via the cast to
unsigned char before assigning to the unsigned char destination.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Apr 8 '07 #4
pete142 wrote:
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"pete142" <pete...@gmail.comwrote in message

news:11**********************@b75g2000hsg.googleg roups.com...
>>When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];
ipString[0] = (BYTE) 0xff & (ip >24);
Make sure type 'unsigned int' on your system is at least
24 bits wide. The language only requires 16.
s/24/25/, else shifting by 24 bits is undefined.
>> ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;
return ipString;
}
for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."
How can I get rid of these four warnings?
First, make sure there will indeed be no data lost (i.e.
the value will fit into an 'unsigned char'), or that if
it is, it doesn't matter to your program.
Observe that each `&' produces a value in the range 0..255,
guaranteed to fit in `unsigned char'.
>Then you can
use a cast. E.g.:

ipString[3] = (BYTE) (0xff & ip);

Use casts with caution. They will often inhibit compiler
warnings that can notify you of things you need to know.

-Mike

Thanks, Mike. I've done all that you suggested. I am using the
strictest warnings level, and should be able to get a clean, no-
warnings compile out of this code /without/ reducing the warnings
level.

So how can I get rid of these warnings, please? That is, how can I
convince the compiler (using standard-C mechanisms) that I don't need
to be warned about these productions?
Are you sure you rearranged the casts in the way Mike Wahler
illustrated? That is, do they now apply to the entire `&' result
instead of just to the first term? If you've done this correctly
and the compiler is still whining, you may simply have to live
with it: Compilers are allowed to issue all the warnings they like
for whatever capricious reasons strike their fancy, and there is
no guaranteed way to shut them up one hundred percent of the time.
File a bug report with the compiler vendor if you like, but don't
expect a fix in the next five minutes.

If the compiler simply won't stop whining and if you are
absolutely sure its complaint is irrelevant, you should at least
insert a comment explaining why the warnings can be ignored:

/* NOTE: Frobozz Magic C version 2.41-06 warns about
* possible data loss in the next four assignments.
* All the "loss" is in fact intentional and caused
* by AND-ing with 0xff, so FMC is a Nervous Nellie.
* -- pete142, 08-Apr-2007
*/

(This comment is rather verbose, but brevity is not the important
point here. You need to provide *all* the relevant information:
Which compiler, which version, what the complaint is about, why
the complaint can be ignored, who says so, and when the analysis
was made. All this will be invaluable to the poor slob who, a
year and a half from now, starts getting warnings when trying to
port the code to SadistiC on the DeathStation 9000.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Apr 8 '07 #5

"pete142" <pe*****@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>"pete142" <pete...@gmail.comwrote in message

news:11**********************@b75g2000hsg.googleg roups.com...
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];
ipString[0] = (BYTE) 0xff & (ip >24);

Make sure type 'unsigned int' on your system is at least
24 bits wide. The language only requires 16.
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;
return ipString;
}
for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."
How can I get rid of these four warnings?

First, make sure there will indeed be no data lost (i.e.
the value will fit into an 'unsigned char'), or that if
it is, it doesn't matter to your program. Then you can
use a cast. E.g.:

ipString[3] = (BYTE) (0xff & ip);

Use casts with caution. They will often inhibit compiler
warnings that can notify you of things you need to know.

-Mike

Thanks, Mike. I've done all that you suggested. I am using the
strictest warnings level, and should be able to get a clean, no-
warnings compile out of this code /without/ reducing the warnings
level.

So how can I get rid of these warnings, please? That is, how can I
convince the compiler (using standard-C mechanisms) that I don't need
to be warned about these productions?
That depends upon the specific warning, but for the one
you're asking about, a cast should do it. Are you still
getting a warning on that line?

-Mike
Apr 9 '07 #6
On Apr 8, 9:14 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"pete142" <pete...@gmail.comwrote in message
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
"pete142" <pete...@gmail.comwrote in message
>news:11**********************@b75g2000hsg.googleg roups.com...
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsigned int ip)
{
static BYTE ipString[4];
ipString[0] = (BYTE) 0xff & (ip >24);
Make sure type 'unsigned int' on your system is at least
24 bits wide. The language only requires 16.
ipString[1] = (BYTE) 0xff & (ip >16);
ipString[2] = (BYTE) 0xff & (ip > 8);
ipString[3] = (BYTE) 0xff & ip;
return ipString;
}
for every assignment statement, I get the warning: "conversion from
'unsigned int ' to 'unsigned char ', possible loss of data."
How can I get rid of these four warnings?
First, make sure there will indeed be no data lost (i.e.
the value will fit into an 'unsigned char'), or that if
it is, it doesn't matter to your program. Then you can
use a cast. E.g.:
ipString[3] = (BYTE) (0xff & ip);
Use casts with caution. They will often inhibit compiler
warnings that can notify you of things you need to know.
-Mike
Thanks, Mike. I've done all that you suggested. I am using the
strictest warnings level, and should be able to get a clean, no-
warnings compile out of this code /without/ reducing the warnings
level.
So how can I get rid of these warnings, please? That is, how can I
convince the compiler (using standard-C mechanisms) that I don't need
to be warned about these productions?

That depends upon the specific warning, but for the one
you're asking about, a cast should do it. Are you still
getting a warning on that line?

-Mike
Now that I've inserted your line as you actually showed it (enclosing
parens), rather than the line as I imagined and read it, everything
works perfectly.

Thank you, Mike. And thank you also Jack and Eric.

-- Pete

Apr 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mja674s | last post: by
3 posts views Thread by Craig Kenisston | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.