Search code examples
cgccruntime-errorturbo-c

Runtime error while compiling


Why do i get runtime eror when running this code in gcc compiler?? It works fine in turboC. I have been working on this for couple of days with no progress at all. Kindly help.

#include<stdio.h>

int main()
{
int T,i=0,num,sum,temp,j;
int *N;
scanf("%d",&T);
while(i++<T)
{
scanf("%d",N);
temp=*N;
while((*N)-->0)
{

    sum=0;
    num=(*N)+1;
    for(j=0;j<64;j++)
    {
    if(num&1)
        sum+=1;
    num=num>>1;
    }
if(((*N)+1+sum)==temp)
    break;
}
printf("%d\n",((*N)<0)?-1:(*N)+1);

}

return 0;

}


Solution

  • include the header file

    # include <stdlib.h>
    

    Allocate memory to the pointer before using it.

    int *N;
    N = (int *)malloc (sizeof (int));
    

    Free the allocated memory after using it

    free (N);