用C语言澄清指针

发布于 2025-01-19 00:47:04 字数 323 浏览 1 评论 0原文

这是我的代码的一个片段。我想知道我正在使用的指针以及我使用它们的方式是否有效。该代码有效,但我需要更多关于它们如何工作的说明。

代码如下:

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

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

发布评论

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

评论(1

年华零落成诗 2025-01-26 00:47:04

假设您

max = *x;

是指*X而不是*P,那么它基本上与分配的起作用基本相同,

*y = max;

首先是指针x被指定为值x指向。然后将该值分配给(相当不错)max变量。

Assuming you mean

max = *x;

with *x instead of *p, then it works basically the same as the assignment

*y = max;

First the pointer x is dereferenced to get the value x is pointing to. Then that value is assigned to the (rather badly named) max variable.

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