473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?

Mar 19 '06 #1
7 1563
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

Mar 19 '06 #2
al*****@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 <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Mar 19 '06 #3
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/>

Mar 19 '06 #4
On Sun, 19 Mar 2006 15:53:24 UTC, "A. Sinan Unur"
<1u**@llenroc.ude.invalid> wrote:
al*****@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!
Mar 19 '06 #5
"Herbert Rosenau" <os****@pc-rosenau.de> wrote in news:wmzsGguTDN6N-pn2-
jG**********@URANUS1.DV-ROSENAU.DE:
On Sun, 19 Mar 2006 15:53:24 UTC, "A. Sinan Unur"
<1u**@llenroc.ude.invalid> wrote:
al*****@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 <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Mar 19 '06 #6
On 19 Mar 2006 07:37:12 -0800, al*****@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
Apr 3 '06 #7
"Dave Thompson" <da*************@worldnet.att.net> wrote in message
news:8q********************************@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
Apr 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RichG | last post by:
I have a problem where DB2 is returning error 'DIA9999E An internal error occurred. Report the following error code : "error code -32"'. This is happening on DB2 for Windows, v7.2.9 when compiling...
13
by: AFKAFB | last post by:
Hi Sometimes when i edit previously saved VBA code e.g. To update field names etc the revised code does not work. Even if i copy and paste in a previous version it does not work. The only...
5
by: n_o_s_p_a__m | last post by:
Can't compile. Does this mean that all functions that throw exceptions must be of return type void? examples: // won't compile: "not all code paths return a value" public override int Run() {...
26
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio...
8
by: mastermagrath | last post by:
Hi, I'm about half way through Bruce Eckels thinking in C++ Vol 1. He gives a very short example of how compiler function name decoration helps remove subtle bugs by type-safe linkage. However,...
15
by: Neo | last post by:
Hello All, I found that ASP.net website only accepts code withing site directory. This creates big hurdle in shairng code. How to share code between two websites, like the way share between two...
13
by: jc | last post by:
I have written a parser using bison and flex to read ASAP2 file for CAN communications. entire development was done in an unix environment and now the code is ready to be integrated to an existing...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
10
by: Keith Halligan | last post by:
I'm switching from the HP-UX compiler aCC 5.55 to aCC 6.13 and I get the following error: "templateptr.h", line 20: error #2289: no instance of constructor "defclass::base::base" matches the...
4
by: Michael Starberg | last post by:
- If it compiles, it works. - If it compiles, it's correct. - If it runs, it doesn't have any bugs. - If it doesn't have any immediately obvious bugs, it's perfect. - If a bug doesn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.