程序代码
#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;
第9行“i<0”写错了