|
From: tim todd on 18 Jun 2008 10:57 how do i do this correctly without warnings? char buf[10]; buf[0] = (char)254; if I say static_cast<char>(254) I get warnings warning C4305: '=' : truncation from 'int' to 'char' warning C4309: '=' : truncation of constant value Thanks! Todd. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin T. on 18 Jun 2008 15:39 tim todd wrote: > how do i do this correctly without warnings? > > char buf[10]; > buf[0] = (char)254; > > if I say static_cast<char>(254) I get warnings > > warning C4305: '=' : truncation from 'int' to 'char' > warning C4309: '=' : truncation of constant value > The reason is that char is a signed type going from -128 to 127 and cannot hold 254. You may want to try unsigned char. br, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 18 Jun 2008 15:39 tim todd wrote: > how do i do this correctly without warnings? > > char buf[10]; > buf[0] = (char)254; First of all, please forget about the fact that these C-style casts exist in C++ for now. These rarely even help except to shift compiler errors or warnings to runtime misbehaviour. Just don't use them. > if I say static_cast<char>(254) I get warnings > > warning C4305: '=' : truncation from 'int' to 'char' > warning C4309: '=' : truncation of constant value The problem is that a 'char' can't hold a value of 254, simple as that. I guess the range of 'char' on your system is -128 to 127. The question now is not how to get 254 into that, but rather what you want to express and then chose the right type and not 'char'. My guess is that you want to express a byte-buffer, for which 'unsigned char' would be much better. Uli -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 19 Jun 2008 07:01 Martin T. wrote: > char is a signed type No, whether char is signed is implementation defined and systems where it is unsigned are even common, PowerPC platforms, for example. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin Bonner on 19 Jun 2008 07:08 On Jun 19, 7:39 am, "Martin T." <0xCDCDC...(a)gmx.at> wrote: > tim todd wrote: > > how do i do this correctly without warnings? > > > char buf[10]; > > buf[0] = (char)254; > > > if I say static_cast<char>(254) I get warnings > > > warning C4305: '=' : truncation from 'int' to 'char' > > warning C4309: '=' : truncation of constant value > > The reason is that char is a signed type going from -128 to 127 s/is/may be/ An implementation is at liberty to have 'char' be an unsigned type. > and cannot hold 254. .... on the OP's platform. > You may want to try unsigned char. Absolutely. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: If the initialization list fail, is the destructor called? Next: MPL Placeholder Basics |