First, read the code section.
1 # include <stdio. h>
2
3 int main ()
4 {
5 char buffer [bufsiz + 1];
6 file * fpin;
7
8 If (fpin = fopen ("test", "R") = NULL ))
9 {
10 printf ("can't open test. \ n ");
11 return 1;
12}
13
14 While (fgets (buffer, bufsiz, fpin )! = NULL)
15 {
16 if (fputs (buffer, stdout) = EOF)
17 {
18 printf ("fputs error to pipe. \ n ");
19 return 1;
20}
21}
22}
This code will generate a segment error: segmentation fault
After debugging GDB, it is found that the segment error is generated by fgets (buffer, bufsiz, fpin) in the code of line 14th.
Fpin is null.
In programming, the following methods may easily cause segment errors, which are basically caused by incorrect pointer usage.
1) to access the system data zone, especially to write data to the memory address protected by the system, the most common is to give a pointer A 0 address
2) memory out of bounds (array out of bounds, variable types inconsistent, etc.) access to areas not in your memory
So find out why fpin is null.
Take a closer look at the 8th lines of code
If (fpin = fopen ("test", "R") = NULL ))
Because the = Priority is higher than = and the = sign is a combination of the Left and the = is a combination of the right, this sentence is equivalent
Fpin = (fopen ("test", "R") = NULL)
That is, the return value of fopen is compared with that of null, and the returned null is copied to fpin,
Therefore, this statement does not check the fpin pointer.
Therefore, the 8th line of code should be
If (fpin = fopen ("test", "R") = NULL)
The running result is normal:
ZYX @ ZYX-Lenovo :~ /Desktop/pipe $ CAT test
Hello, AA!
The second line!
I know I can!
You can too!
ZYX @ ZYX-Lenovo :~ /Desktop/pipe $./segment
Hello, AA!
The second line!
I know I can!
You can too!
Appendix C Operator priority table:
C language operator classification level 1 priority (left) () parentheses; [] subscript operator;-> point to struct member operator;. struct member operator. Level 2 priority (right combination )! Logical non-operator ;~ Bitwise anti-operator; + + prefix increment operator; -- prefix subtraction operator; + positive operator;-negative operator; (type) type conversion operator; * pointer operator; & Address operator; sizeof length operator. Level 3 priority (left Union) * multiplication operator;/division operator; % remainder operator. Level 4 priority (left combination) + addition operator;-subtraction operator. Level 5 priority (left Union) <left shift operator;> right shift operator. Level 6 priority (left join) <, <=,>,> = Relational operators. Level 7 priority (left join) = equals operator ;! = Not equal to the operator. Level 8 priority (left join) & bitwise AND operator. Level 9 Priority (left Union) ^ bitwise XOR or operator. Level 10 priority (left Union) | bitwise OR operator. Level 11 priority (left join) & logic and operators. Level 12 priority (left join) | logic or operator. Level 13 priority (right combination )? : Conditional operator. Level 14 priority (right combination) =, + =,-=, * =,/=, % =, & =, ^ =, |=, <=, >>= value assignment operator. Level 15 priority (left join), comma operator.