Sign in to follow this  

C code with malloc doesn't compile!

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, in the following code, the compiler returns the message: "invalid conversion from `void*' to `int*' " and the code doesn't compile!
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int *p;
  p = malloc(40 * sizeof(int));

...

So, I have tried this: p = (int)malloc(40 * sizeof(int)); and it doesn't compile too! What's wrong?

Share this post


Link to post
Share on other sites
o_O

My understanding is that you're not supposed to need to cast returns from malloc() any more, and that in fact you shouldn't for "good style" any more (because it will "hide the error of not including stdlib.h"; I don't know the details there though).

How old is your compiler?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
o_O

My understanding is that you're not supposed to need to cast returns from malloc() any more, and that in fact you shouldn't for "good style" any more (because it will "hide the error of not including stdlib.h"; I don't know the details there though).

How old is your compiler?


Zahlman, I'm using the new version of DevC++ (4.9.9.0) that comes with newer mingw.

Share this post


Link to post
Share on other sites
Aha. fallenang3l is right then. Use an actual C compiler for C code if you can; try not to be messing around with malloc() and free() in a C++ environment. Bad mojo.

Share this post


Link to post
Share on other sites

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this