weisen@eniac.seas.upenn.edu (Neil Weisenfeld) (07/10/90)
Maybe I should read K&R a little more thoroughly before posting this, but...
Why does the following segment of code give a conversion between
integral types warning under MSC6.00?
void strmem(char *mem, char *str, int n)
{
if (str && mem)
while (n--)
*mem++=(*str)?(*str++):ACTUAL_SPACE;
}
where ACTUAL_SPACE is defined:
#define ACTUAL_SPACE ' '
The *mem++= line gets the error. Since everything is a char, I'm not
sure what the problem is.
Thanks in adv.,
Neil
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Neil I. Weisenfeld | InterNet: weisen@eniac.seas.upenn.edu
Dept. of Computer and Info. Sciences | USPS: I dunno, I'm moving...
University of Pennsylvania | PENNmail: Don't even try it...
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
bobmon@iuvax.cs.indiana.edu (RAMontante) (07/10/90)
weisen@eniac.seas.upenn.edu (Neil Weisenfeld) <26854@netnews.upenn.edu> : | Why does the following segment of code give a conversion between | integral types warning under MSC6.00? | | void strmem(char *mem, char *str, int n) | { | if (str && mem) | while (n--) | *mem++=(*str)?(*str++):ACTUAL_SPACE; | } K&R2 indicates that the conditional operator ( ? : ) expects a logical expression in the initial position, and logical expressions evaluate to integers. Try casting the (*str) to (int) type; that might silence it. Disclaimer: I haven't tried my TC, so I don't know what MSC actually does :-)
norm@oglvee.UUCP (Norman Joseph) (07/10/90)
In <26854@netnews.upenn.edu> weisen@eniac.seas.upenn.edu (Neil Weisenfeld) writes: >Why does the following segment of code give a conversion between >integral types warning under MSC6.00? >#define ACTUAL_SPACE ' ' >void strmem(char *mem, char *str, int n) >{ > if (str && mem) > while (n--) > *mem++=(*str)?(*str++):ACTUAL_SPACE; >} >The *mem++= line gets the error. Since everything is a char, I'm not >sure what the problem is. ^^^^^^^^^^^^^^^^^^^^^^^^^^ Guess again. Everything -isn't- a char. Specifically, the character constant ' ' has as its type -int- whose value is the code for the corresponding character in the target character set (32 decimal in ASCII). I've never used MSC 6.0, but I'd assume that its warning you about the implicit conversion of this int value to the char value on the left hand side of the assignment above (*mem++). Since, by definition, the value of a character constant expression is representable as a char, so you can safely ignore the "warning". On the other hand, since ignoring compiler warning messages is a bad habit to fall into, you can cast the character constant as a char in the assignment: *mem++ = ( *str ) ? *str++ : (char)ACTUAL_SPACE; (Note: you -could- do this cast in the #define line as: #define ACTUAL_SPACE (char)' ' but there may be places in the code where ACTUAL_SPACE is used in an int context.) -- Norm Joseph // Internet: cgh!amanue!oglvee!norm@dsi.com Oglevee Computer Systems, Inc. // Uucp: {pitt,cgh}!amanue!oglvee!norm -- "Whaddya mean he had bullet holes in his mirror?" -- -- Norm Joseph // Internet: cgh!amanue!oglvee!norm@dsi.com Oglevee Computer Systems, Inc. // Uucp: {pitt,cgh}!amanue!oglvee!norm -- "Whaddya mean he had bullet holes in his mirror?" --
coy@ssc-vax.UUCP (Stephen B Coy) (07/10/90)
In article <26854@netnews.upenn.edu>, weisen@eniac.seas.upenn.edu (Neil Weisenfeld) writes: > *mem++=(*str)?(*str++):ACTUAL_SPACE; > > The *mem++= line gets the error. Since everything is a char, I'm not > sure what the problem is. K&R Classic p. 39 "...every char in an expression is automatically converted to and int." Casting ACTUAL_SPACE to char should clear up the warning. Stephen Coy uw-beaver!ssc-vax!coy Brain dead is forever.
kdq@demott.COM (Kevin D. Quitt) (07/10/90)
In article <26854@netnews.upenn.edu> weisen@eniac.seas.upenn.edu (Neil Weisenfeld) writes: >Maybe I should read K&R a little more thoroughly before posting this, but... > >Why does the following segment of code give a conversion between >integral types warning under MSC6.00? > >void strmem(char *mem, char *str, int n) >{ > if (str && mem) > while (n--) > *mem++=(*str)?(*str++):ACTUAL_SPACE; >} > >where ACTUAL_SPACE is defined: > >#define ACTUAL_SPACE ' ' > The ? operator evaluates as type int unless explicity cast. When in doubt as to the source of a warning, try putting each component of the statement on a different line: *mem++= (*str)? (*str++): ACTUAL_SPACE; -- _ Kevin D. Quitt demott!kdq kdq@demott.com DeMott Electronics Co. 14707 Keswick St. Van Nuys, CA 91405-1266 VOICE (818) 988-4975 FAX (818) 997-1190 MODEM (818) 997-4496 PEP last 96.37% of all statistics are made up.
steve@taumet.com (Stephen Clamage) (07/10/90)
weisen@eniac.seas.upenn.edu (Neil Weisenfeld) writes: >Maybe I should read K&R a little more thoroughly before posting this, but... Yes. >Why does the following segment of code give a conversion between >integral types warning under MSC6.00? >#define ACTUAL_SPACE ' ' > *mem++=(*str)?(*str++):ACTUAL_SPACE; The type of a character constant (' ' in your example) is int, not char. Hence the left side of the colon is type char and the right side is int. The compiler is correct in issuing the warning. -- Steve Clamage, TauMetric Corp, steve@taumet.com
weisen@eniac.seas.upenn.edu (Neil Weisenfeld) (07/11/90)
In article <26854@netnews.upenn.edu> weisen@eniac.seas.upenn.edu (Neil Weisenfeld) writes: >Why does the following segment of code give a conversion between >integral types warning under MSC6.00? > >void strmem(char *mem, char *str, int n) >{ > if (str && mem) > while (n--) > *mem++=(*str)?(*str++):ACTUAL_SPACE; >} > >where ACTUAL_SPACE is defined: > >#define ACTUAL_SPACE ' ' > And the winner is.... that person who told me to cast the ' ' to a char. K&R indicates that all character constants are int's. Who ever said to make (*str) into (*str == 0): didn't help. (also changes the sense of the expression). Thanks for all of the responses. Neil =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Neil I. Weisenfeld | InterNet: weisen@eniac.seas.upenn.edu Dept. of Computer and Info. Sciences | USPS: I dunno, I'm moving... University of Pennsylvania | PENNmail: Don't even try it... =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=