473,839 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to make these warnings disappear?


When I compile this code:

typedef unsigned char BYTE;
BYTE *
IpString(unsign ed 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 1914

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

typedef unsigned char BYTE;
BYTE *
IpString(unsign ed 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...@mkwah ler.netwrote:
"pete142" <pete...@gmail. comwrote in message

news:11******** **************@ b75g2000hsg.goo glegroups.com.. .
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsign ed 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...@mkwah ler.netwrote:
"pete142" <pete...@gmail. comwrote in message

news:11******** **************@ b75g2000hsg.goo glegroups.com.. .
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsign ed 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.l earn.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...@mkwah ler.netwrote:
>"pete142" <pete...@gmail. comwrote in message

news:11******* *************** @b75g2000hsg.go oglegroups.com. ..
>>When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsi gned 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.goo glegroups.com.. .
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"pete142" <pete...@gmail. comwrote in message

news:11******* *************** @b75g2000hsg.go oglegroups.com. ..
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsign ed 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...@mkwah ler.netwrote:
"pete142" <pete...@gmail. comwrote in message
On Apr 8, 12:29 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"pete142" <pete...@gmail. comwrote in message
>news:11******* *************** @b75g2000hsg.go oglegroups.com. ..
When I compile this code:
typedef unsigned char BYTE;
BYTE *
IpString(unsign ed 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
11044
by: Gary | last post by:
Hi! guys, I have a SQL agent job fails because it gets 10 warnings when it runs a stored procedure. These warnings are trivial and can be ignored. Can I make it ignore these warnings and proceed? I think there is some setting I can do to change the default behavour of an agent job regarding warnings but I just don't know how to do it. Any idea?
0
1675
by: Steve Barker | last post by:
Hi guys! In Visual Studio .NET, I like to enable auto-generation of XML documentation on the project properties screen, by adding an XML file name under "Configuration Properties" --> "Build" --> "XML Documentation File". This way, I get warnings in the task list about public methods I have written that do not have XML comments (launched by typing "///") associated with them. This works really well, and I get a list of methods I still...
2
2914
by: mja674s | last post by:
I'm making a date picker. It shows up fine. The problem is that I want it to disappear if I click outside the calendar itself. However, if I click *on* the calendar itself, it should not disappear, allowing me to click a date or cycle through the months, etc. Thanks for help. Matt
12
4895
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
3
1753
by: Craig Kenisston | last post by:
Hi, This is a silly question. I'm relatively new to asp.net and asp at all. But I always had this doubt. I'm working with Visual Studio and while exploring the ASPCommerce starter kit, when I switch from design view to html code view, I see many keywords underlied, like if they were invalid, however, they're not, they display fine and the project runs fine.
34
4893
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader Try Set up the database read and do it. Catch ex as system.exception exception stuff here Finally
15
6544
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then i added this line "" before my custom control class (i dont know what this line does). Now
2
1464
by: chupcha | last post by:
Hello all, I'm very new to java script and I need help. I have a couple of buttons on a page that I need to disappear when one of the buttons is clicked. What I found is this: onclick="this.setStyle('display', 'none') It works great, but only for the button that is clicked. How would I make it for all the buttons to disappear?
3
1744
by: Peted | last post by:
Hi Im using vs2008 c# 3.0 and am going through some other person(s) code to clean up compiler warnings that are bieng produced. I have this code (at bottom) where a delegate/event is defined and it builds ok with no errors but im trying to get rid of the
0
9855
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
9697
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,...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10586
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7017
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
5682
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...
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3134
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.