C 中的 mymethod(i) 和 mymethod(&i) 有什么区别?

发布于 2024-10-20 16:18:54 字数 126 浏览 1 评论 0原文

我想知道调用以下方法之间有什么区别:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海似她心 2024-10-27 16:18:55

因为 C 总是按值调用,所以如果您希望函数能够更改函数本身内部的 x,则必须传递 x 的地址。

mymethod(x);

将传递 x,例如,如果 x 是 2,您也可以编写 mymethod(2)

mymethod(&x)

将地址传递给 x。现在该方法可以更改存储在该地址的值,因此函数完成后,x 的实际值可能已更改。

现在你还可以声明一个指针:

int* y; //y is now a pointer to a memory address
y = &x; //y now points to the memory address of x;
*y = 5; will set the "value found at the address y" to 5, thus will set x to 5;

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.

mymethod(x);

will pass x, for example if x is 2, you could as well have written mymethod(2)

mymethod(&x)

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:

int* y; //y is now a pointer to a memory address
y = &x; //y now points to the memory address of x;
*y = 5; will set the "value found at the address y" to 5, thus will set x to 5;
Spring初心 2024-10-27 16:18:55

mymethod(x) 传入整数 x 作为参数。 mymethod(&x) 传入x 内存中的地址。如果您需要一个指向 int 的指针作为参数,那么您将使用第二个。

mymethod(x) passes in the integer x as a parameter. mymethod(&x) passes in the address in memory of x. If you required a pointer-to-int as an argument, then you would use the second one.

似狗非友 2024-10-27 16:18:55

简而言之,当你声明一个指针时,你在它前面加一个星号:

int *ptr;

当你传递&x而不是x时,你正在传递内存地址。

请阅读关于指针的有用介绍

问候。

In few words, when you declare a pointer, you precede it by an asterisk:

int *ptr;

When you pass &x instead of x, you are passing the memory address.

Please, read this useful introduction to the pointers.

Regards.

拥醉 2024-10-27 16:18:55

在行中:
整数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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文