Home > front end >  Segmentation error in the coding pocess is too risky
Segmentation error in the coding pocess is too risky

Time:03-10

Can someone tell me why this code is not working anymore? IT was working perfectly fine yesterday now its giving me segmentation fault(core dump) (This is just filler text because i dont know what to explain anymore, i wrote this code some days ago and it was working perfectly however now its giving me a segmentation error core dump and i dont know the cause to that sadly, so please if anyone could help me and guide me so i can fix this error i would be greateful)

#include<Coding>

CodePudding user response:

An expression in a condition in your loop

for(int i=0; *(arr i)<=length; i  ) //WE TAKE i<=length BECAUSE THE STRLEN FUNCTION DOESNT COUNT \0

*(arr i) means a value of the array arr at offset i, or simply arr[i] that is easier on the eyes.

Now, why does arr[i] must be less than or equal to length?

Also your comment on that line makes no sense. Although the terminating 0 is not counted, you don't need to convert it to Morse code. In fact, there is no encoding for \0. So the condition should be < length.

Oh, and you don't need to invent a function for getting the length of the string, there are couple of them available.

On the other hand, you don't even need to know the length of your string, just go until you see that \0.

CodePudding user response:

instead of using pointer ptr use the name of the array

  • Related