f



code that compiles in c but not in c++?

Hi,is there a code that can compile in c but not in c++ and does not
use any c++ keywords as identifiers? I suspect using void* works in c
but not in c++. Any suggestions?

0
al.cpwn (25)
3/19/2006 3:37:12 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

7 Replies
957 Views

Similar Articles

[PageSpeed] 6

al.c...@gmail.com wrote:
> Hi,is there a code that can compile in c but not in c++ and does not
> use any c++ keywords as identifiers?

Yes, there are plenty of examples, here is a simple one:

int * iptr = malloc(10);  /* Cast required in C++, frowned on in C */

C and C++ are two different languages and I wouldn't recommend trying
to write code that works in both.  However, if you need to know how to
write C++ compatible C code, check out "C: A Reference Manual" fifth
edition.

Robert Gamble

0
rgamble99 (794)
3/19/2006 3:47:03 PM
al.cpwn@gmail.com wrote in news:1142782632.488578.102490
@i40g2000cwc.googlegroups.com:

> Hi,is there a code that can compile in c but not in c++ and does not
> use any c++ keywords as identifiers? I suspect using void* works in c
> but not in c++. Any suggestions?

Trivial:

D:\Home\asu1\UseNet\clc> cat t.c
#include <stdlib.h>

int main(void) {
    char *p = malloc(100);
    *p = 42;
    return 0;
}

D:\Home\asu1\UseNet\clc> gcc -Wall -W -c t.c

D:\Home\asu1\UseNet\clc> g++ -Wall -W -c t.c
t.c: In function `int main()':
t.c:4: error: invalid conversion from `void*' to `char*'


-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
0
1usa (108)
3/19/2006 3:53:24 PM
Robert Gamble wrote:
> al.c...@gmail.com wrote:
> 
>> Hi,is there a code that can compile in c but not in c++ and does
>> not use any c++ keywords as identifiers?
> 
> Yes, there are plenty of examples, here is a simple one:
> 
> int * iptr = malloc(10);  /* Cast required in C++, frowned on in C */
> 
> C and C++ are two different languages and I wouldn't recommend
> trying to write code that works in both.  However, if you need to
> know how to write C++ compatible C code, check out "C: A Reference
> Manual" fifth edition.

And talk to P.J. Plauger, the only contributor here who is known to
have a similar need.

-- 
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on 
 "show options" at the top of the article, then click on the 
 "Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

0
cbfalconer (19194)
3/19/2006 6:24:09 PM
On Sun, 19 Mar 2006 15:53:24 UTC, "A. Sinan Unur" 
<1usa@llenroc.ude.invalid> wrote:

> al.cpwn@gmail.com wrote in news:1142782632.488578.102490
> @i40g2000cwc.googlegroups.com:
> 
> > Hi,is there a code that can compile in c but not in c++ and does not
> > use any c++ keywords as identifiers? I suspect using void* works in c
> > but not in c++. Any suggestions?
> 
> Trivial:
> 
> D:\Home\asu1\UseNet\clc> cat t.c
> #include <stdlib.h>
> 
> int main(void) {
>     char *p = malloc(100);
>     *p = 42;
>     return 0;
> }
> 
> D:\Home\asu1\UseNet\clc> gcc -Wall -W -c t.c
> 
> D:\Home\asu1\UseNet\clc> g++ -Wall -W -c t.c
> t.c: In function `int main()':
> t.c:4: error: invalid conversion from `void*' to `char*'

This is a C program and will compile using a C compiler. You have to 
change something to make a C++ from. Go out of the door and then left.
There is a group discussing ANSI C++. Ask there how to use memory 
assignment in C++. C and C++ are highly different languages even as 
some of theyr basics identical.


-- 
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
0
os2guy1 (1090)
3/19/2006 9:18:54 PM
"Herbert Rosenau" <os2guy@pc-rosenau.de> wrote in news:wmzsGguTDN6N-pn2-
jGpjz0BiKH26@URANUS1.DV-ROSENAU.DE:

> On Sun, 19 Mar 2006 15:53:24 UTC, "A. Sinan Unur" 
> <1usa@llenroc.ude.invalid> wrote:
> 
>> al.cpwn@gmail.com wrote in news:1142782632.488578.102490
>> @i40g2000cwc.googlegroups.com:
>> 
>> > Hi,is there a code that can compile in c but not in c++ and does 
>> > not use any c++ keywords as identifiers? I suspect using void* 
>> > works in c but not in c++. Any suggestions?
>> 
>> Trivial:
>> 
>> D:\Home\asu1\UseNet\clc> cat t.c
>> #include <stdlib.h>
>> 
>> int main(void) {
>>     char *p = malloc(100);
>>     *p = 42;
>>     return 0;
>> }
>> 
>> D:\Home\asu1\UseNet\clc> gcc -Wall -W -c t.c
>> 
>> D:\Home\asu1\UseNet\clc> g++ -Wall -W -c t.c
>> t.c: In function `int main()':
>> t.c:4: error: invalid conversion from `void*' to `char*'
> 
> This is a C program and will compile using a C compiler. You have to 
> change something to make a C++ from. Go out of the door and then left.
> There is a group discussing ANSI C++. Ask there how to use memory 
> assignment in C++. C and C++ are highly different languages even as 
> some of theyr basics identical.

The OP asked to see code that would compile as C, but not as C++. I 
provided an example.

What is your problem?

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
0
1usa (108)
3/19/2006 9:44:37 PM
On 19 Mar 2006 07:37:12 -0800, al.cpwn@gmail.com wrote:

> Hi,is there a code that can compile in c but not in c++ and does not
> use any c++ keywords as identifiers? I suspect using void* works in c
> but not in c++. Any suggestions?

void* itself works in C++, but implicit 'down' conversion from it does
not. (Although some of the things you would use cv void* for in C,
like generic lists, might be _better_ done in C++ with templates.)

For a complete (I believe) list of things in C90/95 that don't work or
are different in C++(98) see Annex C.1 of the C++ standard. If you
can't afford the USD18 to buy it from ANSI, there are several draft
versions available in the 'papers' section of the committee website
http://www.open-std.org/jtc1/sc22/wg21/ . You can't rely on these
being exactly correct or the same as the official standard everywhere,
but for answering general questions they are good enough. Be sure to
consider as you read whether there are any features of C++ you think
should be changed, or described (documented) differently, so that your
download counts as part of the standards development process. <G?>

The obvious and most widespread are oldstyle (nonprototype) function
definitions and declarations, and implicit int and implicit function
declaration. There are more obscure ones like:
  char x [sizeof('x') - 1]; /* legal on most (not all) C
implementations but never on C++ */

C99 adds a number of features that are not in C++, at least not yet as
far as I have tracked. The big ones that spring to mind: long long
types, literals, operations, and formats; stdint.h, inttypes.h, and
other added formats (z, hh, etc.); complex and imaginary numbers and
operations; designated initializers and compound literals; variable
length arrays and flexible array members (of structs).

- David.Thompson1 at worldnet.att.net
0
4/3/2006 2:11:08 AM
"Dave Thompson" <david.thompson1@worldnet.att.net> wrote in message 
news:8qr032l294vb4qufsrp7ell78th4p5kd2s@4ax.com...

> C99 adds a number of features that are not in C++, at least not yet as
> far as I have tracked. The big ones that spring to mind: long long
> types, literals, operations, and formats;

de facto there on many implementations, approved for the next version
of the C++ Standard

>                                          stdint.h, inttypes.h, and
> other added formats (z, hh, etc.);

added with library TR1, which has been approved

>                                   complex and imaginary numbers and
> operations;

complex long since there as templates, now reconciled with C-style
complex via TR1

>             designated initializers and compound literals;

under consideration

>                                                            variable
> length arrays and flexible array members (of structs).

probably will never happen, since such things are better handled
other ways in C++

You overlooked the C99 additions to the preprocessor, which have
also been picked up for the next version of C++.

Bottom line, the list of gratuitous differences *has* shrunk and
continues to shrink.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


0
pjp (713)
4/3/2006 3:35:28 AM
Reply: