I have written a code in UNIX which is complied by using g++. Compling with turbo C didnt yield any errors, but with g++ I am getting Memory fault, core dumped. Could anyone help me out with this?
Given below is the code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
main()
{
long int lt;
int i,r=0,p,m=0,q=0;
char a[100];
char *str;
//a=(char*) malloc(sizeof(char) *100);
str=(char *) malloc(sizeof(char) *100);
scanf("%s",str);
p=strlen(str);
for(i=0;i<p;i++)
{
if(isdigit(str)==0)
{
if((str!='.')&& str!='-')
{
m=1;
}
}
}
for(i=0;i<=p;i++)
{
if(str=='-')
r=r+1;
}
for(i=0;i<=p;i++)
{
if(str=='.')
q=q+1;
}
lt=atol(str);
if(p>=16 || strcmp(str,NULL)==0 || m==1 || r>1 ||q>1 || lt==0)
{
printf("ERROR");
}
else
{
//ltoa(lt,a,10);
sprintf(a,"%l",lt);
printf("%s",a);
}
}