how to make these warnings disappear?

P

pete142

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
 
M

Mike Wahler

pete142 said:
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
 
P

pete142

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
 
J

Jack Klein

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
 
E

Eric Sosman

pete142 said:
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.)
 
M

Mike Wahler

pete142 said:
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
 
P

pete142

pete142 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top