Author Topic: C::B compiles in K&R C and not ANSI C...?  (Read 18765 times)

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
C::B compiles in K&R C and not ANSI C...?
« on: May 19, 2009, 07:13:27 pm »
Hello everyone :)

I think that my C::B is compiling in K&R and not in ASNI C..

ie. with this code
Code
#include <stdlib.h>

int main (void) {

    char *pfilename;
    pfilename = malloc( 1 );

return 0;
}

returns
Quote
error: invalid conversion from `void*' to `char*'

Of course this
Code
pfilename = (char*)malloc( 1 );
compiles without errors.. But this is not ANSI C right?


I tried selecting an other compiler from C::B option but not luck.
Even tried the devC++ compiler but nothing..

Any ideas? what is wrong?


Thank you very much!

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #1 on: May 19, 2009, 07:46:39 pm »
The "flavor" of C being compiled is a function of the compiler, not the IDE.  Depending upon the compiler you choose and the options you select (specific to that compiler) you will get different modes of compiling.  Note that (AFAIK) the Dev-C++ IDE uses only the GCC compiler.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #2 on: May 19, 2009, 08:03:00 pm »
Yes i am aware of that :)

But i have GNU GCC Compiler selected at C::B compiler options and i tried the same flags..

So i guess there is also something else i need to change in the IDE's option.

Thx for the reply

Offline AlexN

  • Multiple posting newcomer
  • *
  • Posts: 16
    • Alex's Link Sammlung
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #3 on: May 19, 2009, 08:31:20 pm »
Quote
error: invalid conversion from `void*' to `char*'

Of course this
Code
pfilename = (char*)malloc( 1 );
compiles without errors.. But this is not ANSI C right?


Thank you very much!

I my opinion this is behavior is more ANSI C then K&R. ANSI C checks the type, K&R C assigns all to all.  :)

Alex  ;)
best regards
 Alex ;)

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #4 on: May 19, 2009, 08:37:48 pm »
well ANSI C does not need any casting to mallocs

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #5 on: May 19, 2009, 08:51:48 pm »
By default the GCC C-compiler operates in "C99" mode, which does require a cast of one pointer type to another (in this case a "void *" to a "char *").  C99 is neither K&R nor ANSI, but has elements of both.

However, at this point we are no longer talking about C::B, but the multitude of C standards out there.  You could get better help by asking the guys on USENET on the "comp.lang.c" list.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #6 on: May 19, 2009, 09:04:57 pm »
r u sure about this?

Anyway.. Even so the problem still remains..

Not solved even when i put the -ansi flag at the compiler options.

So i still think it has something to do with C::B. I hadn't that problem with DevC++ or Pelles C.


Thanks

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #7 on: May 19, 2009, 09:39:36 pm »
Actually, I copied your original code into a brand new C::B "console" project, and it compiled and ran just fine.  No problems at all.  Apparentely the GCC optimizer is smart enough to translate a "void *" into a "char *" with no problems.

Are you sure you are compiling a "C" project, and not a "C++" project?  Is your main program named "main.c" or "main.cpp" ?

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #8 on: May 19, 2009, 09:48:18 pm »
(!) (!)
Yes i am.. I posted that only this small part for illustration reasons.

the whole program is much bigger with not other errors.. Just 2 errors of this kind..

What is more i reinstalled C::B and same happened.. :(
any ideas?


thx 4 helping :)

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #9 on: May 19, 2009, 10:02:22 pm »
When I create a default C-language project, the only compiler options I have checked is optimization "O2", and your code snippet compiled good.

What options do you have checked?  Also, what version of GCC are you using?  My system is using GCC 3.4.4

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #10 on: May 19, 2009, 10:17:21 pm »
not sure.. in the options its GNU GCC Compiler

i have tried with various flags checked. and i just tried only with checking O2 but no difference

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #11 on: May 19, 2009, 10:38:20 pm »
Even with -pedantic warnings turned on, I cannot get it to fail:


mingw32-gcc.exe -pedantic -W -Wall -ansi  -O2     -c C:\temp\test1\main.c -o obj\Release\main.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

 
Can you show us your full compiler log output?

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #12 on: May 19, 2009, 10:57:42 pm »
sure, but its actually just that
Code
\f.c        In function `int main()':
\f.c   6   error: invalid conversion from `void*' to `char*'
             === Build finished: 1 errors, 0 warnings ===

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #13 on: May 19, 2009, 11:18:26 pm »
Please turn on full-command-line logging (Settings -> Compiler and Debugger -> Other Settings then "Compiler Logging") then try to compile again.  Copy that log output into here, so we can see what all (hidden) settings are being used.

Also, can you open a command window (for example, a DOS window or a MSys xterm) and type "gcc -v" to see what version of GCC you are using.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #14 on: May 19, 2009, 11:35:44 pm »
ok i enabled full-command-line logging but the message is exactly the same as above..

i open a cmd window and type gcc -v but it is not found..(i guess because its not in the windows directory)