• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

.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
0
Canon
Asked:
Canon
  • 5
  • 3
  • 3
  • +1
1 Solution
 
fridomCommented:
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
 
fridomCommented:
Well the cast is not needed in C but needed in C++ therefor my question from above.

Regards
Friedrich
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
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
 
fridomCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now