c 面试题,关于指针与数组的应用!
main(){
int a[6],*i,*p=a,*t=a;
printf("qing shu ru shu ju:\n");
for(i=a;i<a+6;i++)
{scanf("%d",i);}
for(i=a;i<a+6;i++)
{
*p=*i+*t;
printf("%4d",*p);
}
getch();
}
输入:1 2 3 4 5 6
输出:2 4 7 11 16 22
什么原理呀?
6 5 for( i =a; i < a+6 ; i ++ ) (gdb) 9 for( i = a; i < a+6 ; i ++ ) (gdb) 11 *p = (*i) + (* t); (gdb) display *i 1: *i = 1 (gdb) display *t 2: *t = 1 (gdb) n 12 printf("%4d",*p); 2: *t = 2 1: *i = 2 (gdb) p a $2 = {2, 2, 3, 4, 5, 6} (gdb) n 9 for( i = a; i < a+6 ; i ++ ) 2: *t = 2 1: *i = 2 (gdb) n 11 *p = (*i) + (* t); 2: *t = 2 1: *i = 2 (gdb) 12 printf("%4d",*p); 2: *t = 4 1: *i = 2 (gdb) p a $3 = {4, 2, 3, 4, 5, 6}