Segmentation fault (core dumped)?
程序代码
#include"stdio.h"
void main()
{
int a[5];
int *p;
int i;
for(i<0;i<5;i++);
{
a[i]=i+1;
p=a;
}
/*下标法输出数组元素*/
for(i=0;i<5;i++)
{
printf("a[%d]:%-8d",i,a[i]);
printf("\n");
}
/*地址法输出数组元素*/
for(i=0;i<5;i++)
{
printf("*(a+%d):%-8d",i,*(a+i));
printf("\n");
}
/*下标法输出数组元素*/
for(i=0;i<5;i++)
{
printf("p[%d]:%-8d",i,p[i]);
printf("\n");
}
/*地址法输出数组元素*/
for(i=0;i<5;i++)
{
printf("*(p+%d):%-8d",i,*(p+i));
printf("\n");
}
}
运行结果
root@huangzihan:/program/c_program# ./output_array_elements.out
Segmentation fault (core dumped)
Gdb调试
root@huangzihan:/program/c_program# gcc output_array_elements.c -g -o output_array_elements.out
root@huangzihan:/program/c_program# ls -a
. .. core input_average.c input_average.out output_array_elements.c output_array_elements.out
root@huangzihan:/program/c_program# gdb ./output_array_elements.out core
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./output_array_elements.out...
warning: core file may not match specified executable file.
[New LWP 85018]
Core was generated by `./output_array_elements.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x000055e9c1e4a1bb in main () at output_array_elements.c:11
11 a[i]=i+1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第9行“i<0”写错了