Segmentation fault (core dumped)

以下程序不小心在11行scanf()中把parr写成了*parr,在编译时没问题,但在运行时出现:

[lujinhong@lujinhong chapter12]$ gcc reverse3.c
[lujinhong@lujinhong chapter12]$ ./a.out
Enter 10  number: 19 76 8 93 766 8 6 934 0 -3
Segmentation fault (core dumped)


[cpp] view plain copy
  1. #include <stdio.h>  
  2.   
  3. #define ARRAY_SIZE 10  
  4.   
  5. int main(void){  
  6.   
  7.     int arr[ARRAY_SIZE], *parr;  
  8.   
  9.     printf("Enter %d  number: ", ARRAY_SIZE);  
  10.     for(parr=arr; parr<arr+ARRAY_SIZE; parr++){  
  11.         scanf("%d", *parr);  
  12.     }  
  13.   
  14.     printf("The number will output in reversal order:\n");  
  15.     for(parr=arr+ARRAY_SIZE-1; parr>=arr; parr--){  
  16.         printf("%d\t",*parr);  
  17.     }  
  18.     printf("\n");  
  19.   
  20.     return 0;  
  21. }   

Segmentation fault (core dumped)一般是对内存操作不当造成的,常见的有:

(1)数组超出范围。

(2)修改了只读内存。

(3)还有本例也是修改了只读内存。

你可能感兴趣的:(fault,segmentation)