![]() |
Segmentation fault in a program ! Help !
Code:
#include<stdio.h>need help with this program . this programe is giving compile error:- SEGMENTATION FAULT |
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.
|
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:
Code:
Code:
#include<stdio.h> |
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++)Sometimes an assignment in an if is what you really want. Take the following: Code:
int i=0; |
| All times are GMT +5.5. The time now is 21:27. |