文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Segmentation fault (core dumped

Segmentation fault (core dumped

时间:2009-05-07  来源:hubo214

#include <stdio.h>

main(viod)
{
  int i,n;
  int  Array[n];
  scanf("%d\n",&n);
  for(i=0;i<=n;i++)
      Array[n]=i;
  printf("Array[n]=%d\n",Array[n]);
}
编译通过:
hubo@hubo:~/c_programming$ gcc -g -o 5 5.c
5.c:11:2: warning: no newline at end of file
但是执行时出错:
hubo@hubo:~/c_programming$ ./5
Segmentation fault (core dumped)

做一个修改后:
#include <stdio.h>

main(viod)
{
  int i,n,j;
  j=scanf("%d",&n);
  int  Array[n];
  for(i=0;i<=n;i++)
      Array[n]=i;
  printf("Array[%d]=%d\nj=%d\n",n,Array[n],j);
}
编译执行:
hubo@hubo:~/c_programming$ gcc -g -o 5 5.c
5.c:11:2: warning: no newline at end of file
hubo@hubo:~/c_programming$ ./5
10
Array[10]=10
j=1

segmantation fault :最主要的错误就是声明了指针,但是没有初始化 ,结果再后来的时候进行间接引用 ,就出现问题了。

#include <stdio.h>
#include <string.h>
main()
{
   char *p ;
   char test[] = "hello";
   strcpy(p,test);
   printf(p);
}

编译通过:
hubo@hubo:~/c_programming$ gcc -g -o 6 6.c
6.c:9:2: warning: no newline at end of file
但执行时:
hubo@hubo:~/c_programming$ ./6
Segmentation fault (core dumped)
修改原程序:对p赋初值,char *p=NULL;

相关阅读 更多 +
排行榜 更多 +
味子夫

味子夫

购物比价 下载
恩猫

恩猫

购物比价 下载
街头纷争

街头纷争

动作格斗 下载