#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.?