2

Any idea why is this happening here?

float *image;
long size_img=par->N*par->M;

image = calloc(size_img, sizeof(float));//the compiler shows error here

The error is

error: a value of type "void *" cannot be assigned to an entity of type "float *"

Should I do a cast? The bogus thing is that I do the same elsewhere on the program and that error is not shown.

I have this as part of a struct I named par

long *tbegin;

and then I do

par->tbegin = calloc( SUMA_J, sizeof ( long ) );

And I get no error.

Atirag
  • 1,660
  • 7
  • 32
  • 60

1 Answers1

4

What is happening here is that most likely you are inadvertently compiling your code as C++ code. In C++ (as opposed to C) void * is not implicitly convertible to other pointer types.

It you intend to write your code in C, make sure you compile it as C. It you intend to write your code in C++, you will have to use explicit type conversion operators to convert void * pointers to other pointer types.

The conversion to long * pointer type probably happens in other translation unit (i.e. other file), which is compiled as C. That is probably why it succeeds.

Note that compilers can use C and C++ mode independently for each translation unit. Many compilers will choose between C and C++ based on file extension, compiling .c files as C and .cpp files as C++.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • The strange thing is that it works on the other case. I have been using gcc to compile but now I'm using nvcc because I have some portions of the code with Cuda. Could it be that nvcc is using gcc on the cases that work and nvcc where it doesn't work? – Atirag Jul 02 '13 at 01:30
  • @Atirag: Where is that "other case" located? Same file? Other file? As I said above, the choice of language is made on per-file basis, which is why it can easily work in one file (compiled as C) and fail to work in another file (compiled as C++). What are your file's extensions? `.c`? `.cpp`? Something else? What are the command line switches you pass to the compiler for each file? – AnT stands with Russia Jul 02 '13 at 01:31
  • Separate files. The other case is on a .c file and the error case is on a .cu file so I guess that's it. Thanks a lot. – Atirag Jul 02 '13 at 01:33
  • 1
    Well, apparently the compiler you use treats `.cu` extension as C++ extension. It is not necessary to change the extension though (if you don't want to). You can usually force the compiler to use a specific language by specifying a command-line switch. – AnT stands with Russia Jul 02 '13 at 01:34