|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- int *f(int *s)
- {
- s+=1;
- s[1]+=6;
- *s++ +=7;
- return s;}
- void main()
- {
- int a[5]={1,2,3,4,5},*p;
- p=f(&a[1]);
- printf("%d,%d,%d,%d",a[1],a[2],*p,p[1]);
- }
复制代码
子函数不是很理解,可以详细地讲讲吗??谢谢各位啦
- #include<stdio.h>
- int * f(int * s)
- {
- s += 1 ; // s 本来指向 a[1],加 1 后,指向 a[2]。
- s[1] += 6 ; // s[1] 实际上等效于 * [s + 1] = * [s + 1] + 6,也就是 a[3] = a[3] + 6 = 4 + 6 = 10
- * s ++ += 7 ; // s 指向 a[2],此句等效于 a[2] = a[2] + 7 = 3 + 7 = 10,然后 s 指向 a[3]
- return s ; // 返回 s 指向 a[3]
- }
- int main(void)
- {
- int a[5] = {1 , 2 , 3 , 4 , 5} , * p ;
- p = f (& a[1]) ; // 函数参数指向 a[1],返回 p 指向 a[3]
- printf("%d , %d , %d , %d" , a[1] , a[2] , * p , p[1]) ; // a[1] = 2,a[2] = 10,* p = a[3] = 10,p[1] = a[4] = 5
- }
复制代码
|
|