3
votes

my code shows this warnings when compiling with "g++ -Wall -pedantic -Wno-long-long -c main.c". I have to compile in this mode, becouse its a homework and we have an application that corrects them and it uses this compile mode.

  1. Error: invalid conversion from "void" to "int** " [-fpermissive]
  2. Error: invalid conversion from "void" to "int* " [-fpermissive]
  3. Error: invalid conversion from "void" to "main(int, char*)::VYSLEDEK" [-fpermissive]

the same errors continue as i realloc quite a lot in my program. I tried to change almost everything in that realloc, it is still the same.

Parts of the code :

    struct VYSLEDEK
{
    int sirka;
    int vyska;
    int zacatek_x;
    int zacatek_y;
    int soucet;
} *vysledek;
    int **matice,**soucty;

    .....       

    matice=(int**)malloc(1*sizeof(int*));
    matice[0]=(int*)malloc(1*sizeof(int));
    soucty=(int**)malloc(1*sizeof(int*));
    soucty[0]=(int*)malloc(1*sizeof(int));

    .....

1.      matice=realloc(matice,naalokovano*2*sizeof(int*));
2.      soucty=realloc(soucty,naalokovano*2*sizeof(int*));

    .....

        for (i=0;i<(naalokovano*2);i++)
        {
3.            matice[i]=realloc(matice[i],sizeof(int));
4.            soucty[i]=realloc(soucty[i],sizeof(int*));
        };

    .....

5.      vysledek=realloc(vysledek,vysledku*sizeof(struct VYSLEDEK*));

Thank you for your help.

1
In C, never cast on right hand side of assignment: i.e. drop the (int**) cast from (int**)malloc. It's unnecessary and makes code harder to maintain. Note though that you're using a C++ compiler. - Bathsheba
By using g++ you're compiling your C code as C++. You need to cast the return from realloc to do this. Your calls to malloc give examples of how to do this. - simonc
@Bathsheba Isn't this actually C++ though (since g++ is being used as the compiler)? - simonc
@simonc; yes, he's using a C++ compiler (possibly accidentally) but his language tag and question text imply C. - Bathsheba
The question suggests that use of a C++ compiler is deliberate - "its a homework and we have an application that corrects them and it uses this compile mode". I'd presume the C tag is the mistake, not the compiler command line - simonc

1 Answers

3
votes

You already did cast the result of malloc to the right type. Do the same for the realloc calls, too.

BTW: Don’t complain that they force you to switch on the warnings. I think it’s the most sensible thing to do by default, I always use at least -Wall -Werror.