输出结果是多少呢?
#include <stdio.h>void change_p(int *pi)
{
*pi+=2;pi+=2;*pi+=2;
}
int
main()
{
int *p,a[]={1,4,7,10};
p=&a[1];change_p(p);
printf("%d\n",*p);
}
我觉得是12啊~~
可运行后是6啊
谁帮我解惑~~
[此贴子已经被作者于2006-1-10 17:10:55编辑过]
6
调用函数change_p():到这句“*pi+=2”指针p和pi指向的都是a[1],此时a[1]和*p和*pi的值都是6.到这句“pi+=2”指针pi就移向a[3],而指针p指向不动 还是指向a[1]。到这句“*pi+=2;”*pi和a[3]==10+2是12了。*p还是6
#include <stdio.h>
int change_p(int *pi)
{
*pi+=2;pi+=2;*pi+=2;
return *pi;
}
int main()
{
int *p,a[]={1,4,7,10},b;
p=&a[1];b=change_p(p);
printf("*p=%d\n",*p);
printf("b=*pi=%d\n",b);
printf("a[1]=%d\n",a[1]);
printf("a[3]=%d\n",a[3]);
return 0;
}