Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Invalid conversion" error when using malloc? [duplicate]

Tags:

c

pointers

matrix

Possible Duplicate:
invalid conversion from void*' to char*' when using malloc?

I'm trying to allocate a matrix dinamically on the memmory using pointers, but I keep receiving the error messages:

|122|error: invalid conversion from 'void*' to 'int**'|

|124|error: invalid conversion from 'void*' to 'int*'|

Here is my code, I can't see what I'm doing wrong... this "void*" conversion does not make sense for me...

   int i,j;
   int **a;
   int c = 2;
 

   /* Form the matrix */
   a = malloc((nxy+1)*sizeof(int *));
   for (i=0;i<=nxy;i++)
      a[i] = malloc((nxy+1)*sizeof(int));
   
   for (i=0;i<=nxy;i++)
      for (j=0;j<=nxy;j++)
         a[i][j] = 0;
   
like image 605
GennSev Avatar asked Mar 14 '26 05:03

GennSev


1 Answers

You are compiling your program with a C++ compiler. You have two choices:

  1. Don't do that. Use a C compiler.
  2. Cast the return value from malloc().
like image 178
Carl Norum Avatar answered Mar 16 '26 19:03

Carl Norum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!