C 中的 mymethod(i) 和 mymethod(&i) 有什么区别?
我想知道调用以下方法之间有什么区别:
int x;
mymethod(x);
和
mymethod(&x);
I was wondering what the difference in is between calling a method like :
int x;
mymethod(x);
and
mymethod(&x);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为 C 总是按值调用,所以如果您希望函数能够更改函数本身内部的 x,则必须传递 x 的地址。
将传递 x,例如,如果 x 是 2,您也可以编写
mymethod(2)
将地址传递给 x。现在该方法可以更改存储在该地址的值,因此函数完成后,x 的实际值可能已更改。
现在你还可以声明一个指针:
Because C always does call-by-value, if you want the function to be able to change the x inside the function itself, you have to pass the address of x.
will pass x, for example if x is 2, you could as well have written
mymethod(2)
will pass the address to x. Now the method can change the value stored at that address, so after the function is completed, the actual value of x might have been changed.
Now you can also declare a pointer:
mymethod(x)
传入整数x
作为参数。mymethod(&x)
传入x
内存中的地址。如果您需要一个指向 int 的指针作为参数,那么您将使用第二个。mymethod(x)
passes in the integerx
as a parameter.mymethod(&x)
passes in the address in memory ofx
. If you required a pointer-to-int as an argument, then you would use the second one.简而言之,当你声明一个指针时,你在它前面加一个星号:
当你传递
&x
而不是x
时,你正在传递内存地址。请阅读关于指针的有用介绍。
问候。
In few words, when you declare a pointer, you precede it by an asterisk:
When you pass
&x
instead ofx
, you are passing the memory address.Please, read this useful introduction to the pointers.
Regards.
在行中:
整数x;
你分配了一块“int”大小的内存。
为了这个解释,我们假设 int 的大小是 4 个字节(它可能不是 4 个字节)。
所以在“int x;”行之后内存中有 4 个字节分配给“x”。
“x”的值在这 4 个字节内:
例如,如果 x=4 那么它将在内存中看起来像这样:
[0, 0, 0, 4] 或二进制 [0000000, 00000000, 00000000, 00000010]。
(在现实生活中,它也可能是 [4, 0, 0, 0],但我不会深入讨论)。
所以 x 的 VALUE 是 4。
但是假设我想要“x”的地址,它在内存中的位置。
这是运算符“&”的位置开始发挥作用,使用此运算符我请求 x 的地址。
因此,如果 [0, 0, 0, 4] 从内存中的位置“0x12341234”开始,&x 将返回该位置(0x12341234)。
现在,如果我想将地址存储在变量中,则该变量的类型不是“int”,而是指向被标记为“int*”的int的地址。
所以:
int x = 4; // <-- 分配 4 字节内存并用数字 4 填充其值。
int* 指向 x 的指针 = &x; // <--pointer_to_x 指向 x 在内存中所在的地址。
如果有一个像这样声明的方法:
void mymethod(int x) 比我们传递 x 的值,因此该方法被称为 mymethod(x)。
如果有一个像这样声明的方法:
void mymethod(int* x) 比我们传递一个指针到 x 的地址,所以该方法被称为 mymethod(&x)。
这确实是冰山一角,我真的尽力保持简单,所以如果您还有其他问题,请尽管问!
还有一些术语称为“按值”和“按引用”,但您仍然需要更好地理解 int 和 int* 之间的区别,并且“按值”和“按引用”会很自然。
In the line:
int x;
you allocate a piece of memory in the size of "int".
for this explanation we will assume the size of int is 4 bytes (it might not be 4 bytes).
so after the line "int x;" there are 4 bytes in the memory assigned to "x".
the value of "x" is inside these 4 bytes:
for example, if x=4 then it will look in the memory like this:
[0, 0, 0, 4] or in binary [0000000, 00000000, 00000000, 00000010].
(In real life it could also be [4, 0, 0, 0], but I won't get into that).
so the VALUE of x is 4.
But lets say I want the address of "x", where it is placed in the memory.
This is where the operator "&" comes into play, using this operator I request the address of x.
so if [0, 0, 0, 4] starts at the location "0x12341234" in the memory, &x will return that (0x12341234).
Now, if I want to store the address in a variable, this variable's type is not "int", but it is something that points to the address of int which is being marked as "int*".
So:
int x = 4; // <-- allocates memory of 4 bytes and fills its value with the number 4.
int* pointer_to_x = &x; // <-- pointer_to_x points to the address where x is located in the memory.
if there is a method declared like so:
void mymethod(int x) than we pass THE VALUE of x, so the method is being called mymethod(x).
if there is a method declared like so:
void mymethod(int* x) than we pass a POINTER to the address of x, so the method is being called mymethod(&x).
It is really the tip of the iceberg, and I really tried to keep it simple as I could, so if you have further questions, just ask!
There are also terms called "by value" and "by reference" but you still need to understand better the difference between int and int* and than "by value" and "by reference" will be quite natural.