这三者的区别是什么? [英] Whats the difference b/w these three..

查看:69
本文介绍了这三者的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a)


main()

{

int i = 5,j = 2;

208让我们C

垃圾(i,j);

printf(\ n%d%d,i,j);

}

垃圾(int i,int j)

{

i = i * i;

j = j * j;

}

(b)



main()

{

int i = 5,j = 2;

junk(& i,& j);

printf(\ n%d%d,i,j);

}

垃圾(int * i,int * j)

{

* i = * i * * i;

* j = * j * * j;

}





c)main()

{

int i = 4,j = 2;

垃圾(& i,j);

printf(\ n%d%d,i,j);

}

junk(int * i,int j)

{

* i = * i * * i;

j = j * j;

}



我的尝试:



我的理解,第一个描述了va的呼叫lue和second是通过引用打电话......但是这是怎么回事。

plz describe



ans

a )5 2

b)25 4

c)16 2

解决方案

当你在C中传递一个值时,它每次都按值传递。

这意味着复制了外部世界值,并将该副本发送给该函数。函数对值的任何更改都会对值的副本进行更改,并且一旦函数结束,就不会在外部世界中反映出来。



这就是为什么这个版本:

<前lang =c ++>垃圾(i,j);
printf( \ n%d%d,i,j);
...
垃圾( int i, int j)
{
i = i * i;
j = j * j;
}

打印值,就像没有调用函数一样。



当你将值的地址传递给a时函数,它是作为参数复制和传递的地址,而不是它是地址的项目。把它想象成汽车:你总是把车停在同一个停车位,所以你可以通过说第17排,12号槽告诉某人你的车在哪里,他们就能找到你的车。但是,无论车辆在哪里,车的地址都有效:当你告诉你的朋友你的车在哪里并给他钥匙时,你不要复制你的车!

所以当你尝试这个功能时:

垃圾(& i,j); 
printf( \ n%d%d,i,j);
...
junk( int * i, int j)
{
* i = * i * * i;
j = j * j;
}

函数接收的值是i的地址副本和j值的副本。对指针 i 地址变量的更改会影响外部世界中的变量,更改为 j don'的值t,因为它是价值的副本。

回到汽车,你给你的伴侣钥匙和停放地点的地址。如果他在驾驶汽车时撞坏了汽车,那么汽车会造成损坏,而不是汽车的副本,因为你传递了汽车地址的副本,而不是汽车本身的副本。



现在有意义吗?


a)
main( )
{
int i = 5, j = 2 ;
208 Let Us C
junk ( i, j ) ;
printf ( "\n%d %d", i, j ) ;
}
junk ( int i, int j )
{
i = i * i ;
j = j * j ;
}
(b)

main( )
{
int i = 5, j = 2 ;
junk ( &i, &j ) ;
printf ( "\n%d %d", i, j ) ;
}
junk ( int *i, int *j )
{
*i = *i * *i ;
*j = *j * *j ;
}


c)main( )
{
int i = 4, j = 2 ;
junk ( &i, j ) ;
printf ( "\n%d %d", i, j ) ;
}
junk ( int *i, int j )
{
*i = *i * *i ;
j = j * j ;
}

What I have tried:

what I understood that ,,first one describes call by value and second is call by reference...but how this happening .
plz describe

ans
a) 5 2
b) 25 4
c) 16 2

解决方案

When you pass a value in C, it is passed by value, every time.
What that means is that the "outside world" value is copied, and that copy is sent to the function. Any changes the function makes to the value are made to the copy of the value, and are not reflected in the "outside world" once the function ends.

That's why this version:

   junk ( i, j ) ;
   printf ( "\n%d %d", i, j ) ;
...
junk ( int i, int j )
   {
   i = i * i ;
   j = j * j ;
   }

Prints the values as if the function had not been called.

When you pass the address of a value to a function, it is the address that is copied and passed as the parameter, not the item it is the address of. Think of it like cars: you always park your car in the same parking space, so you can tell someone where you car is by saying "row 17, slot 12" and they can find your car. But the "address of the car" works regardless of what car is there: when you tell your friend where your car is and give him the keys, you don't make a copy of your car!
So when you try this function:

   junk ( &i, j ) ;
   printf ( "\n%d %d", i, j ) ;
...
junk ( int *i, int j )
   {
   *i = *i * *i ; 
   j = j * j ;
   }

The values the function receives are a copy of the address of i, and a copy of the value of j. Changes to the variable the pointer i addresses affect the variable in the outside world, changes to the value of j don't, because it's a copy of the value.
Back to cars, and you gave your mate the keys and the address of where you parked it. If he crashes the car while he drives it, the damage is done to your car, not to a copy of it because you passed a copy of the address of the car, not a copy of the car itself.

Make sense now?


这篇关于这三者的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆