Segmentation fault (core dumped)?

发布于 2022-09-12 23:50:44 字数 2440 浏览 30 评论 0

程序代码

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2022-09-19 23:50:45

第9行“i<0”写错了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文