我正在尝试对指向整数的指针数组进行排序(而不是整数数组本身)
但是当我尝试初始化指向整数数组中整数地址的指针数组时,我的程序崩溃了。
int** pointerSort(int* arr, int size)
{
int i;
// allocate memory for result array and verify success of allocation
int** res = (int**)malloc(size*sizeof(int*));
if (res = NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
// initialize pointers array with addresses
for (i = 0; i < size; i++)
res[i] = &(arr[i]);
// sort the array using merge sort algorithm
mergeSort(res, size-1);
return res;
}
我的程序崩溃了res[i] = &(arr[i]);