Memory Fault,Core dumped

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);
}
}

I did not look further but... in your second and third for-loop, the counter goes from 0 to p, both inclusive. It should be

for(i=0; i<p ;i++)

Hi,

Your code is giving segmentation fault due to wrong use of strcmp(str1,str2) function.

A string can't be compared using strcmp with a NULL pointer. Both the arguments should be not NULL only. Use

str==NULL

for comparison instead.
This 'll remove the segmentation fault. :slight_smile:

Regards,
Vinod.