用C语言澄清指针
这是我的代码的一个片段。我想知道我正在使用的指针以及我使用它们的方式是否有效。该代码有效,但我需要更多关于它们如何工作的说明。
代码如下:
void swapValues(int *x, int *y) {
int max;
max = *x;
*x = *y;
*y = max;
}
int 变量 max
可以等于 *x
吗?我理解 *y = max;
的工作原理,但我对第一个感到困惑。你们能详细说明一下吗,以便我能掌握它是如何工作的。
this is a snippet from my code. I'm wondering if the pointers I'm using and the way I'm using them is valid. The code works but I need more clarification on how they work.
Here's the code:
void swapValues(int *x, int *y) {
int max;
max = *x;
*x = *y;
*y = max;
}
The int variable max
can be equal to *x
? I understand how the *y = max;
works but I'm confused in the first one. Can y'all please elaborate it more so that I can grasp how it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您
是指
*X
而不是*P
,那么它基本上与分配的起作用基本相同,首先是指针
x
被指定为值x
指向。然后将该值分配给(相当不错)max
变量。Assuming you mean
with
*x
instead of*p
, then it works basically the same as the assignmentFirst the pointer
x
is dereferenced to get the valuex
is pointing to. Then that value is assigned to the (rather badly named)max
variable.