.c won't compile keep getting "incompatible types in initializer" error

I keep getting a compile error for the following:

}
Bitmap;

/* construct a new bitmap */
Bitmap *makeBitmap (int width, int height)
{
    Bitmap *result = malloc (sizeof (Bitmap));
    result->width = width;
    result->height = height;
    result->widthBytes = (width + 7) / 8;
    result->buf = malloc (height * result->widthBytes);
    return result;
}

The compile error is: incompatible types in initializer
'Bitmap *result = malloc (sizeof (Bitmap))'

Let me know if you need to see more of the code. I got it off the web and it makes barcodes online:)

Thanks
Canon
CanonAsked:
Who is Participating?
 
peetmCommented:
I've tried compiling the code with the Miracle C compiler you're using and it seems that that compiler has a few problems, i.e., it's not so good!

Why don't you use gcc?  DevCpp is an easy way to set this up:

http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe

0
 
fridomCEO/ProgrammerCommented:
is <stdlib.h> included?

And are you compiling the Code as C code or are you using some C++ compiler?

The usual way of using malloc is
Bitmap *result = malloc(sizeof(*result)); /* BTW */

Regards
Friedrich

0
 
ozoCommented:
#include <stdlib.h>


 Bitmap *result = (Bitmap*) malloc (sizeof (Bitmap));
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
fridomCEO/ProgrammerCommented:
Well the cast is not needed in C but needed in C++ therefor my question from above.

Regards
Friedrich
0
 
peetmCommented:
Almost certainly you're compiling this as C++
0
 
CanonAuthor Commented:
Thanks Everyone. Here is the little barcode.c source code  I am trying to compile:
http://www.milk.com/barcode/
 
I am using a free compiler from here: http://www.reg.net/product.asp?ID=3376

I thought the above would be more simple than posting the whole little code here? I am very new to C

Thanks Canon
0
 
peetmCommented:
Yes, I tried it with gcc in both C and C++ modes.

In C it compiles, in C++ it does not - the line you pointed out initially being the culprit.  In C++ the return type of malloc needs to coerced  with a cast - (Bitmap*) malloc ...

0
 
CanonAuthor Commented:
Hello Peetm,
   Thank you for the link! I am downloading now with a slow dialup connection:(
I will try it out real quick and let you know. Do I need to change anything in the original barcode.c file for this to work or will the program I am downloading compile it with no problem?

Thanks
Canon
0
 
peetmCommented:
You won't need to change a thing - I didn't, and it compiled fine.

Have a look here too - some stuff about setting up DevCpp

http://www.peetm.com/courses/devcpp/
0
 
fridomCEO/ProgrammerCommented:
Well it would be good if you had read what I wrote.
1) is <stdlib.h> included?
2) are you compiling the sources as C cod

C++ compilers must cast the return value of malloc.

So please ensure that the above things are correct and the code will get compiled without any trouble.

Friedrich
0
 
peetmCommented:
>>So please ensure that the above things are correct and the code will get compiled without any trouble.

fridom - no it won't.

The Magic C compiler he's using is naff - I've downloaded the source that he's wanting to compile, and the compiler he's using :- the compiler has many issues, and, once I'd eventually altered the code to get it to compile [which took a while] , it simply crashes.
0
 
CanonAuthor Commented:
Points to Peetm and Thanks for all who tried to help. Great program too peetm!

Thanks Again

Canon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.