how to make these warnings disappear?

Discussion in 'C Programming' started by pete142, Apr 8, 2007.

  1. pete142

    pete142 Guest

    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
    pete142, Apr 8, 2007
    #1
    1. Advertising

  2. pete142

    Mike Wahler Guest

    "pete142" <> wrote in message
    news:...
    >
    > 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
    Mike Wahler, Apr 8, 2007
    #2
    1. Advertising

  3. pete142

    pete142 Guest

    On Apr 8, 12:29 pm, "Mike Wahler" <> wrote:
    > "pete142" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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
    pete142, Apr 8, 2007
    #3
  4. pete142

    Jack Klein Guest

    On 8 Apr 2007 09:43:00 -0700, "pete142" <> wrote in
    comp.lang.c:

    > On Apr 8, 12:29 pm, "Mike Wahler" <> wrote:
    > > "pete142" <> wrote in message
    > >
    > > news:...
    > >
    > >
    > >
    > > > 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
    Jack Klein, Apr 8, 2007
    #4
  5. pete142

    Eric Sosman Guest

    pete142 wrote:
    > On Apr 8, 12:29 pm, "Mike Wahler" <> wrote:
    >> "pete142" <> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >>> 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
    lid
    Eric Sosman, Apr 8, 2007
    #5
  6. pete142

    Mike Wahler Guest

    "pete142" <> wrote in message
    news:...
    > On Apr 8, 12:29 pm, "Mike Wahler" <> wrote:
    >> "pete142" <> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >> > 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
    Mike Wahler, Apr 9, 2007
    #6
  7. pete142

    pete142 Guest

    On Apr 8, 9:14 pm, "Mike Wahler" <> wrote:
    > "pete142" <> wrote in message
    >
    > > On Apr 8, 12:29 pm, "Mike Wahler" <> wrote:
    > >> "pete142" <> wrote in message

    >
    > >>news:...

    >
    > >> > 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
    pete142, Apr 9, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trint Smith
    Replies:
    13
    Views:
    1,478
  2. Naren
    Replies:
    2
    Views:
    2,655
    Chris Theis
    Jan 16, 2004
  3. Walter Dnes (delete the 'z' to get my real address

    What do these warnings mean?

    Walter Dnes (delete the 'z' to get my real address, Jun 5, 2004, in forum: C Programming
    Replies:
    15
    Views:
    604
    Dan Pop
    Jun 10, 2004
  4. Sathyaish

    Why these warnings?

    Sathyaish, Jun 11, 2004, in forum: C Programming
    Replies:
    6
    Views:
    1,023
    Darrell Grainger
    Jun 11, 2004
  5. Ted Sung
    Replies:
    1
    Views:
    301
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page