Segmentation fault in a program ! Help !

Discussion in 'C' started by lionaneesh, Mar 24, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    
        int i,x;
        char c[100];
    
        printf("Enter input :");
        fgets(c,100,stdin);
    
        for( i = 0 ; i <= (x = (strlen(c))) ; i++)
        {
    
            if( c[i] = '\t')
            {
                c[i] = (' ');
                            printf("   ");
            }
            printf("%c",c[i]);
        }
    }
    the aim of this program is that it will replace tabs entered in the input with 4 spaces.

    need help with this program .

    this programe is giving compile error:-

    SEGMENTATION FAULT
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Though I am out of touch with C but as far as I can remember SEGMENTATION FAULT is not a compile time error.
     
  3. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    Your way of coding is making a easy things to difficult one.Also as shabbir said segmentation fault is not a compile time error . It is a run time error .But when I run I didn't get segmentation fault . I got a unterminated loop.


    Code:
        
    for( i = 0 ; i <= (x = (strlen(c))) ; i++)
    
    You can write like this ,

    Code:
       
     for( i = 0 ; i <= strlen(c); i++)
    
    Also you have used '=' for the comparison.You need to use '==' for that.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        int i,x;
        char c[100];
    
        printf("Enter input :");
        fgets(c,100,stdin);
    
        for( i = 0 ; i <= strlen(c); i++)
        {
    
            if( c[i] == '\t')
            {
                            printf("****"); // I used '*' for the understanding , you just change it to space .
            }
            else
            {
            printf("%c",c[i]);
            }
        }
    }
    
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The reason the loop was unending was that in the last iteration of the loop, i.e. when i==strlen(c), c is the terminating NULL, which due to using assign rather than compare you were overwriting with a tab character, then on the next iteration you would start to get undefined behaviour (which often leads to a segfault).

    Remember in C counting starts from 0. So a less than or equal comparison in the loop will count one MORE than you might have intended. Try it: let's count i from 0 to 5 using <=. 0, 1, 2, 3, 4 are all obvious. What about 5? Is 5 less than or equal to 5? YES. So i<=5 will still evaluate TRUE. So i will loop through the values 0,1,2,3,4,5, which is SIX iterations.

    So unless you actually meant for the count to include the terminating NULL, you should use
    Code:
    for (i=0; i<strlen(c); i++)
    
    And ALWAYS be careful about = and ==. Some languages interpret = differently according to the context, for example in BASIC the clause "IF A=5" is taken to be a comparison, but "A=5" is an assignment. But in C = *ALWAYS ALWAYS ALWAYS* means "assign", so "if (a=5)" will assign 5 to a, then evaluate 5 as a logical value where 0 is FALSE and any other value is TRUE. Since 5 is non-zero, it is TRUE, so "if (a=5)" will assign 5 to x and what comes after the if will always be performed (and if there is an else, that will never be performed). Some compilers will throw a "condition is always true" warning if they spot this, but it will have to be assignment to a constant.

    Sometimes an assignment in an if is what you really want. Take the following:
    Code:
    int i=0;
    while (c=str[i++])
        ...;
    
    This will also loop over a string. For all non-zero values str will be copied to c and the expression will evaluate TRUE. When it gets to the terminating NULL, str which is 0 will be copied to c, this will evaluate FALSE and the loop will terminate.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice