Re: program that compiles in C but not in C++?

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 01/10/04


Date: Sat, 10 Jan 2004 12:35:30 -0500 (EST)


On Sat, 10 Jan 2004, [iso-8859-1] Nils O. Selåsdal wrote:
>
> In article <btn8v2$olk$1@daisy.noc.ucla.edu>, Digital Puer wrote:
> > I came across a question that asked for me to write a
> > small program that will compile in C but not in C++.
> > Any ideas?
> >
> > The only one I can think of is one that tries to
> > downcast a void * to a more specific pointer without
> > a cast:

> void foo(){
> unsigned char c = 0xfa;
> char *d = &c;
> }

  This certainly *looks* like it would require a diagnostic in C,
as well as in C++. gcc and g++ agree with me; 'unsigned char *'
and 'char *' are not assignment-compatible.

> void foo(){
> char or[] = "or";
> }

  This requires no diagnostic in either language. Maybe you're
thinking of

        char *or = "or";

which is fine in C, but I'm not sure whether C++ still allows this
or not. (I'm guessing it does, for backwards compatibility, but I
could easily be wrong.)

-Arthur



Relevant Pages

  • Re: Are _T() and TEXT() macros equivalent?
    ... are not concerned if a char takes 1 byte or 100. ... since the language defines an object ... various ramifications of using and not using the switch. ... Why the fixation on basing the meaning of the keyword "char" on a compiler ...
    (microsoft.public.vc.mfc)
  • Re: Are _T() and TEXT() macros equivalent?
    ... by every single application developed in that language for the last 30 ... are not concerned if a char takes 1 byte or 100. ... Go through the standard and list the sections that use char as byte. ... void write ...
    (microsoft.public.vc.mfc)
  • Re: An Observation
    ... arcane things that matter only for special purposes. ... You mean like the fact that "CHAR X" means different things depending on ... In a reasonable language, it should be clear which "noun" an "adjective" like CHAR modifies, and the syntactic relationship shouldn't change depending on some implicit state. ...
    (comp.lang.forth)
  • Re: An Observation
    ... there is a problem) is that CHAR doesn't always execute. ... In a reasonable language, it should be clear which "noun" an "adjective" like CHAR modifies, and the syntactic relationship shouldn't change depending on some implicit state. ... So when newbies encounter Forth, they get stuck on elaborate stack gymnastics. ...
    (comp.lang.forth)
  • Re: learning c and c++
    ... >> books on each desired language, more would be better, but of course ... > possible) int to char, ... 'int') If you know that a particular 'int' value is within ... to a 'string'. ...
    (alt.comp.lang.learn.c-cpp)