1. Environment:
centOS 7 virtual machine
2. Code:
tag: tail.c
-sharpinclude<stdio.h>
int my_getline(char *s, int maxlens)
{
int c;
int i;
for(i = 0; i < maxlens && (c=getchar())!= EOF && c != "\n"; iPP)
*sPP = c;
if(c == "\n")
{
*sPP = "\n";
iPP;
}
*sPP = "\0";
return i;
}
3. Question:
compilation will pass, but will occur during execution:
first case:
[root@server0 tail]-sharp ./tail -4
Segmentation fault (core dumped)
[root@server0 tail]-sharp ls
Segmentation fault (core dumped) is output after pressing ctrl + d (EOF)
the second case:
[root@server0 tail]-sharp ./tail
fjgdshafgk
dhfdsjhfj
jfhds
dfhd
jdfh
jfdf
sadfjsasjf
adjfsadfjaskdjfk
njdfnjsda
jjdanfj
jfhdsjfhsasjdsj
1
54[
5;5
55
88"
66
99
66
644
555
66
Segmentation fault (core dumped)
[root@server0 tail]-sharp ls
(tail is an executable program generated after compilation)
could you tell me what"s wrong with this program?