使用指针交换 int 数组值

发布于 2024-08-09 09:15:55 字数 638 浏览 7 评论 0原文

我应该使用指针来交换数组中的整数。它编译时没有错误或警告并运行,但不交换整数。任何建议都会有帮助!

这是测试器:

#import <stdio.h>

void swap( int ary[] );

int main(  int argc, char*argv[] )
{
    int ary[] = { 25, 50 };
    printf( "The array values are: %i and %i \n", ary[0], ary[1] );
    swap( ary );
    printf( "After swaping the values are: %i and %i \n", ary[0], ary[1] );

    return 0;
}

这是交换函数:

void swap( int ary[] )
{
    int temp = *ary;
    *ary = *(ary + 1);
    *ary = temp;
}

这是运行后显示的内容:

The array values are: 25 and 50
After swaping the values are: 25 and 50

I am supposed to use pointers to swap ints in an array. It compiles with no errors or warnings and runs but does not swap the ints. Any suggestions would be helpful!!!

Here is the tester:

#import <stdio.h>

void swap( int ary[] );

int main(  int argc, char*argv[] )
{
    int ary[] = { 25, 50 };
    printf( "The array values are: %i and %i \n", ary[0], ary[1] );
    swap( ary );
    printf( "After swaping the values are: %i and %i \n", ary[0], ary[1] );

    return 0;
}

Here is the swap function:

void swap( int ary[] )
{
    int temp = *ary;
    *ary = *(ary + 1);
    *ary = temp;
}

This is what is displayed after running:

The array values are: 25 and 50
After swaping the values are: 25 and 50

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

¢蛋碎的人ぎ生 2024-08-16 09:15:55

我讨厌破坏这个,但它看起来更像是一个打字错误。

在您的交换函数中:

*ary = temp;

应该是:

*(ary + 1) = temp;

编辑:您不使用数组表示法有原因吗?我认为对于这样的事情来说更清楚一些:

int temp = ary[0];
ary[0] = ary[1];
ary[1] = temp;

I hate spoiling this but it looks like a typo more than anything.

In your swap function:

*ary = temp;

should be:

*(ary + 1) = temp;

edit: Is there a reason you're not using array notation? I think it's a bit clearer for things like this:

int temp = ary[0];
ary[0] = ary[1];
ary[1] = temp;
疯到世界奔溃 2024-08-16 09:15:55

更仔细地检查您的交换函数:

void swap( int ary[] )
{
    int temp = *ary;
    *ary = *(ary + 1);
    *ary = temp;
}

*(ary + 1) 何时被分配

Examine your swap function more carefully:

void swap( int ary[] )
{
    int temp = *ary;
    *ary = *(ary + 1);
    *ary = temp;
}

When does *(ary + 1) get assigned to?

稚然 2024-08-16 09:15:55

将第二个值移至第一个位置,然后将第一个值移回第一个位置。

You move the second value into the first spot, and then move the first value back into the first spot.

愁杀 2024-08-16 09:15:55

只是为了好玩;也可以在不使用临时值的情况下进行交换,

void swap( int ary[] )
{
    *ary ^= *(ary + 1);
    *(ary + 1) ^= *ary;
    *ary ^= *(ary + 1);
}

正如 GMan 指出的那样,这段代码掩盖了编译器和处理器的意图,因此性能可能比使用临时变量更差,尤其是在现代 CPU 上。

just for fun; It's also possible to swap without using a temporary value

void swap( int ary[] )
{
    *ary ^= *(ary + 1);
    *(ary + 1) ^= *ary;
    *ary ^= *(ary + 1);
}

As GMan points out, this code obscures your intent from the compiler and the processor, so the performance may be worse than using a temp variable, especially on a modern CPU.

静若繁花 2024-08-16 09:15:55

您还可以在没有任何临时变量的情况下交换值:

void swap(int *x, int *y)
{
   *x ^= *y;
   *y ^= *x;
   *x ^= *y;
}

然后调用:

swap(&ary[0], &ary[1]);

You can also swap the values without any temporary variable:

void swap(int *x, int *y)
{
   *x ^= *y;
   *y ^= *x;
   *x ^= *y;
}

then call:

swap(&ary[0], &ary[1]);
知足的幸福 2024-08-16 09:15:55

试试这个:

void swap( int ary[] )
{
    int temp = ary[0];
    ary[0] = ary[1];
    ary[1] = temp;
}

Try this instead:

void swap( int ary[] )
{
    int temp = ary[0];
    ary[0] = ary[1];
    ary[1] = temp;
}
水溶 2024-08-16 09:15:55

您的交换函数仅适用于 2-ints 数组,因此请将其显示给您的编译器(它不会改变任何内容,但会使代码更清晰)

void swap( int ary[2] )

your swap function will work only for 2-ints array, so show it to your compiler (it won't change anything, but make code cleaner)

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