问题描述
我写不复制部分归并成一个额外的临时数组。出于这个原因,我创建了一个辅助阵列辅助
I'm writing a mergesort without the copy part into an extra temp array. For this reason, I create an auxiliary array aux
int * aux
aux = (int *) calloc(n, sizeof(int));
其中, N
是数组的大小。在功能合并,我想用自己的指针继续与我的算法来交换在最后两个数组。但是,如果我换的指针,并将其打印到控制台,我得到了奇怪的东西:
where n
is the size of the array. In the function merge, I want to swap both arrays at the end by using their pointer to continue with my algorithm. But if I swap the pointers, and print them to console, I got weird stuff:
在该方法本身的指针交换。但是,如果我我的方法检查后,回到我的主力,指针不再交换。
我的合并方法的调用:
Within the method itself, the pointers are swapped. But if I check after my method, back in my main, the pointers aren't swapped anymore.The call of my merge method:
merge(a, lo, mid, hi, aux);
其中, A
是我的主阵列,辅助
辅助和罗
,中期
和喜
都是整数。
where a
is my main array, aux
the auxiliary and lo
, mid
and hi
are integers.
原型:
void merge(int *a, int lo, int mid, int hi, int *aux);
我试图来交换他们是这样的:
I tried to swap them like this:
int temp;
temp = a;
a = aux;
aux = temp;
您可以帮我解决这个问题?
Can you help me solve this problem?
在此先感谢!
推荐答案
由于指针是按值传递时发生错误。
The error occurs because the pointers are passed by value.
他们更改为指针的指针:
Change them to pointers to pointers:
void merge(int **a, int lo, int mid, int hi, int **aux);
交换是这样的:
int *temp;
temp = *a;
*a = *aux;
*aux = temp;
合并
则应该被称为是这样的:
merge
should then be called like this:
merge(&a, lo, mid, hi, &aux);
这篇关于交换两个数组的指针的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!