Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Segmentation fault in a program ! Help ! (http://www.go4expert.com/forums/segmentation-fault-program-help-t21512/)

lionaneesh 24Mar2010 22:26

Segmentation fault in a program ! Help !
 
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

shabbir 25Mar2010 09:51

Re: Segmentation fault in a program ! Help !
 
Though I am out of touch with C but as far as I can remember SEGMENTATION FAULT is not a compile time error.

karthigayan 25Mar2010 10:09

Re: Segmentation fault in a program ! Help !
 
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]);
        }
    }
}


xpi0t0s 25Mar2010 14:19

Re: Segmentation fault in a program ! Help !
 
The reason the loop was unending was that in the last iteration of the loop, i.e. when i==strlen(c), c[i] 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[i] will be copied to c and the expression will evaluate TRUE. When it gets to the terminating NULL, str[i] which is 0 will be copied to c, this will evaluate FALSE and the loop will terminate.


All times are GMT +5.5. The time now is 09:57.