1
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *ptr;
    int n;

    printf("Enter the no of elements you want in an array1:");
    scanf("%d",&n);
    printf("The no of elements in array 1 would be %d",n);

    ptr=(int*)malloc(n*sizeof(int));
    if(ptr==NULL){
        printf("Memory not alloted ");
    }
    else{
        printf("Memory successfully alloted using malloc");
    }

    printf("The elements of array 1 are:");
    for(int i=0;i<n;i++){
        ptr[i]=2*i;
        printf("%d",ptr[i]);
    }
    free(ptr);
    printf("Memory has been successfully freed\n");

    int *ptr1,n1;
    printf("Enter the no of elements you want in an array2:\n");
    scanf("%d",&n1);
    printf("The no of elements in array 2  would be %d",n1);

    ptr1=(int*)calloc(n1,sizeof(int));
    if(ptr1==NULL){
        printf("Memory not alloted \n");
    }
    else{
        printf("Memory successfully alloted using calloc\n");
    }

    printf("The elements of array 2 are:");
    for(int j=0;j<n1;j++){
        ptr1[j]=j+2;
        printf("%d",ptr1[j]);
    }

    ptr1=realloc(ptr1,2*n1*sizeof(int));
    printf("Memory successfully reallocated using realloc");
    printf("The array after reallocation is:");
    
    for(int k=0;k<2*n1;k++){
        ptr1[k]=k+2;
        printf("%d",ptr1[k]);
    }
    
    return 0;
}

I wrote this code by declaring everything but still I am getting a compile error:

main.cpp:50:17: error: invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
     ptr1=realloc(ptr1,2*n1*sizeof(int));
          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~

Could anyone tell me what is the error and how I should correct it.?

dbush
  • 205,898
  • 23
  • 218
  • 273
Noob_coder
  • 21
  • 1
  • 5
  • 6
    C++ is not C. `*.cpp` is typically the name of a C++ program. The compiler seems to be inferring from the name that you want to compile as C++. – William Pursell Sep 13 '21 at 11:50
  • 1
    Probably the correct solution is `mv main.cpp main.c`. – William Pursell Sep 13 '21 at 11:51
  • try to add on the realloc function (int*) as : `(int*)realloc(ptr1, 2 * n1 * sizeof(int));` – Nevo Goldman Sep 13 '21 at 11:52
  • 1
    Only if they really want C++ instead of C, @NevoGoldman. The two are different languages in many more ways than this, and it is a grave error to compile C code as C++ unless that code was painstakingly crafted to conform to both languages simultaneously -- which is difficult and rarely worth the effort. C or C++: choose **one**. – John Bollinger Sep 13 '21 at 11:57
  • 1
    I don't see any problem with not casting. In fact you also should **not** cast to `(int )*` in the `malloc` and `calloc` lines. – Cheatah Sep 13 '21 at 11:58
  • ... in C, that is. – John Bollinger Sep 13 '21 at 11:58
  • Yes, that was assuming that we're talking C here. I'm going by the tag and the include headers, and assume that @WilliamPursell was correct. – Cheatah Sep 13 '21 at 12:05

2 Answers2

5

The realloc function is returning a void * which you're assigning to a int *. In C, conversions between a void * and any other object pointer can be performed without a cast. However, the name of the file you're compiling has a .cpp extension. Most compilers will see that and compile it as a C++ program, and in C++ a conversion from void * to int * requires a cast.

Since the code appears to be completely C and you've used the C tag, I'm assuming you want to compile a C program. That being the case, rename the main.cpp file to main.c and it should compile properly.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

You compile the C program using C++ compiler. You need to:

  1. If usung g++: add -xc command line option
  2. If msvc: add /Tc or better /TC command line option
  3. Or simply rename .cpp files to .c (it may not work depending on the command line options)

Do not cast or do any other magic. It is very bad to compile C program using C++ compiler as they are different languages and many things differ.

0___________
  • 60,014
  • 4
  • 34
  • 74