简体   繁体   English

C:使用指针反向数组?

[英]C : reverse array using pointers?

I don't see where I have made an error in this code :我没有看到我在这段代码中哪里出错了:

void swap(int* a, int* b)
{
int temp = *a;

*a = *b;
*b = temp;
}

void array_reverse(int *begin, int *end)
{
    int *end2 = end;
    int *q = 0;
    for (q = begin; q < end; q += 1)
    { 
        swap(q, end2); 
        end2 -= 1;
    }
}

it should reverse the array:它应该反转数组:

arr{ 1, 2, 3}

becomes:变成:

arr{ 3, 2, 1}

My output:我的输出:

[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10] 

becomes :变成:

[111009824,  2,  3,  4,  5,  6,  7,  8,  9, 10]

(well actually this first element always changes each time I compile and test my function and gives me random values I guess ) (实际上,每次编译和测试我的函数时,第一个元素总是会发生变化,并给我随机值,我猜)

The issue is with the for loop问题在于 for 循环

void array_reverse(int *begin, int *end)
{
    int *end2 = end;
    int *q = 0;
    for (q = begin; q < end; q += 1)
    { 
        swap(q, end2); 
        end2 -= 1;
    }
}

You must change end to end2 in order to stop when you reach the middle You must also decrement end2 before you call swap so you are pointing at the right value您必须将end更改为end2以便在到达中间时停止您还必须在调用 swap 之前减少 end2,以便您指向正确的值

void array_reverse(int *begin, int *end)
{
    int *end2 = end;
    int *q = 0;
    for (q = begin; q < end2; q += 1)
    { 
        end2 -= 1;
        swap(q, end2); 

    }
}

The function call would then look something like this函数调用看起来像这样

int test[10] = {1,2,3,4,5,6,7,8,9,10};
array_reverse(test, test + 10);

There were two problems.有两个问题。 In the for loop, end should have been end2 .for循环中, end应该是end2

for (q = begin; q < end2; q += 1) {
    swap(q, end2); 
    end2 -= 1; }

The other issue was the call.另一个问题是通话。 It should have been array_reverse (a, a+9);它应该是array_reverse (a, a+9); because array indexes start at 0 .因为array索引从0开始。 Giving a+10 for the second argument with an array of length 10 passes a pointer to nonsense outside the array bounds.为具有长度为 10 的array的第二个参数提供a+10会传递一个指向数组边界之外的无意义的指针。

I must express some appreciation for the question, it got me researching the fundamental difference between "swapping pointer addresses around" and swapping the "data to which the pointers point.我必须对这个问题表示赞赏,它让我研究了“交换指针地址”和交换“指针指向的数据”之间的根本区别。

Something else worth noting is that, in C, function arguments are by-value copies .还有一点值得注意的是,在 C 中, 函数参数是按值复制的 We could rewrite array_reverse like this with no adverse consequences.我们可以像这样重写array_reverse而不会产生不利后果。 Why does this work?为什么这样做?

void array_reverse(int *begin, int *end)
    for ( ;begin < end; ) swap(begin++, end--)

The function body receives local copies of arguments to work on.函数体接收要处理的参数的本地副本 Hence, there is nothing wrong with modifying their values.因此,修改它们的值并没有错。 It is guaranteed that the function can't modify the initial values of its arguments outside the function without simulating a pass by reference through some form of indirection .保证函数不能在函数外部修改其参数的初始值,而不模拟通过某种形式的间接引用传递。 There is a powerful simplicity to grasping the concept.掌握这个概念非常简单。

Here is another way of writing the loop using while .这是使用while编写循环的另一种方法。

void array_reverse(int *first, int *last)
{
    int *f = first;
    int *l = last;
    while (f < l)
    { 
        swap(f, l);
        f++, l--;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM