You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

适配ArbLib的C快速排序swap函数指针类型错误修复求助

ArbLib快速排序适配的指针类型不匹配问题修复方案

问题概述

将Programiz的C语言快速排序实现适配ArbLib的arb_t数据类型时,已将数组类型改为arb_ptr,用arb_le实现<=比较逻辑,但调用swap函数时出现指针类型不匹配警告:期望参数类型为arb_struct (*)[1],实际传入arb_struct *。

错误根源

  1. partition函数参数类型错误:原partition函数的第一个参数定义为arb_t array,而ArbLib中arb_t是struct arb_struct[1]的typedef(本质是长度为1的数组类型)。当通过&array[j]取元素地址时,得到的类型是arb_struct (*)[1],但swap函数期望的参数是arb_t*(即struct arb_struct**),二者类型不兼容。
  2. arb_set参数顺序颠倒:原代码中arb_set(&array[high], pivot)把目标和源搞反了,arb_set的第一个参数是目标变量,第二个是源变量。
  3. arb_le调用冗余取地址:arb_le的参数是const arb_t类型,直接传入array[j]即可,无需额外取地址。

修复步骤

  • 修正partition函数参数类型:将第一个参数改为arb_ptr array,与quickSort函数的参数类型保持一致,这样array[j]是arb_t类型,&array[j]即为arb_t*,匹配swap的参数要求。
  • 修正arb_set的参数顺序:将arb_set(&array[high], pivot)改为arb_set(pivot, &array[high]),确保源和目标正确。
  • 简化arb_le的调用:将arb_le(&array[j], pivot)改为arb_le(array + j, pivot)或arb_le(&array[j], pivot)均可,更简洁的方式是直接用array[j](数组类型可隐式转换为指针)。
  • 添加内存清理:所有临时创建的arb_t变量都需要调用arb_clear释放内存,避免内存泄漏。

完整修正代码

#include "arb.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void swap(arb_t *a, arb_t *b) {
  arb_t t;
  arb_init(t);
  arb_set(t, *a);
  arb_set(*a, *b);
  arb_set(*b, t);
  arb_clear(t); // 清理临时变量内存
}

// function to find the partition position
int partition(arb_ptr array, int low, int high) {
  
  // select the rightmost element as pivot
  arb_t pivot;
  arb_init(pivot);
  arb_set(pivot, &array[high]); // 修正源和目标顺序
  
  // pointer for greater element
  int i = (low - 1);

  // traverse each element of the array
  // compare them with the pivot
  for (int j = low; j < high; j++) {
    if (arb_le(array + j, pivot) == 1) {
        
      // if element smaller than pivot is found
      // swap it with the greater element pointed by i
      i++;
      
      // swap element at i with element at j
      swap(&array[i], &array[j]);
    }
  }

  // swap the pivot element with the greater element at i
  swap(&array[i + 1], &array[high]);
  
  arb_clear(pivot); // 清理pivot内存
  // return the partition point
  return (i + 1);
}

void quickSort(arb_ptr array, int low, int high) {
  if (low < high) {
    
    // find the pivot element such that
    // elements smaller than pivot are on left of pivot
    // elements greater than pivot are on right of pivot
    int pi = partition(array, low, high);
    
    // recursive call on the left of pivot
    quickSort(array, low, pi - 1);
    
    // recursive call on the right of pivot
    quickSort(array, pi + 1, high);
  }
}

// function to print array elements
void printArray(arb_ptr array, int size) {
  for (int i = 0; i < size; ++i) {
    arb_printd(array + i, 6);
    printf("\n");
  }
}

// main function
int main() {
  srand(time(NULL));
  int n = 5;
  arb_ptr data = _arb_vec_init(n);
  for(int i=0;i<n;i++){
      arb_t rnd;
      arb_init(rnd);
      arb_set_d(rnd, (double)(rand()%100)/10);
      arb_set(&data[i], rnd);
      arb_clear(rnd); // 清理临时变量内存
  }
  
  printf("Unsorted Array\n");
  printArray(data, n);
  
  //perform quicksort on data
  quickSort(data, 0, n - 1);
  
  printf("Sorted array in ascending order: \n");
  printArray(data, n);
  
  _arb_vec_clear(data, n); // 清理数组内存
}

内容的提问来源于stack exchange,提问作者Robin to Roxel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 11:55:54