Thread View: comp.sys.ibm.pc.programmer
6 messages
6 total messages
Started by Neil Weisenfeld
Mon, 09 Jul 1990 18:34
`C' type question...
Author: Neil Weisenfeld
Date: Mon, 09 Jul 1990 18:34
Date: Mon, 09 Jul 1990 18:34
30 lines
1353 bytes
1353 bytes
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...
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Re: `C' type question...
Author: Norman Joseph
Date: Tue, 10 Jul 1990 15:12
Date: Tue, 10 Jul 1990 15:12
46 lines
2354 bytes
2354 bytes
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?" --
Re: `C' type question...
Author: Stephen B Coy
Date: Tue, 10 Jul 1990 16:13
Date: Tue, 10 Jul 1990 16:13
14 lines
903 bytes
903 bytes
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.
Re: `C' type question...
Author: Kevin D. Quitt
Date: Tue, 10 Jul 1990 16:22
Date: Tue, 10 Jul 1990 16:22
33 lines
1427 bytes
1427 bytes
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.
Re: `C' type question...
Author: Stephen Clamage
Date: Tue, 10 Jul 1990 16:30
Date: Tue, 10 Jul 1990 16:30
17 lines
1018 bytes
1018 bytes
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
Re: `C' type question...
Author: Neil Weisenfeld
Date: Tue, 10 Jul 1990 22:51
Date: Tue, 10 Jul 1990 22:51
33 lines
1605 bytes
1605 bytes
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...
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads