C-对函数内部的Struct*数组的副本进行排序


struct numz
{
  int num;
};
typedef struct numz_ numz;
int main()
{
  int num_elements = 10;
  numz* myStruct_a = smalloc(sizeof(int)*num_elements;
  for (n = 0; n < num_elements; n++)
    myStruct_a->num[n] = n;
  funct(myStruct_a);
  return 0;
}
numz *funct(numz *myStruct_a)
{
  int num_elements = 10;
  numz* myStruct_a_cpy = smalloc(sizeof(int)*num_elements;
  for (n = 0; n < num_elements; n++)
  {
    myStruct_a_cpy->num[n] = myStruct_a->num[n];
  }
  //PSEUDO CODE
  //REARRANGE THE ELEMENTS IN myStruct_a_cpy
  return myStruct_a_cpy;
}

为什么myStruct_a_cpy没有重新排列?

为什么要用smalloc(sizeof(int)*num_elements)初始化myStruct_a_cpy?不应该是sizeof(myStruct)吗?

另外,为什么不直接使用qsort呢?

int compare(const void*a,const void*b({myStruct*struct_a=(myStruct*(a;myStruct*struct_b=(myStruct*(b;//比较两个结构的内容,//如果<b、 如果a>b则为1,否则为0。}

myStruct*函数(myStruct*myStruct_a(

{
    int num_elements = 10;
    myStruct* myStruct_a_cpy = smalloc(sizeof(int)*num_elements);
    for (n = 0; n < num_elements; n++)
    {
        myStruct_a_cpy[n] = myStruct_a[n]
    }
    qsort(myStruct_a_cpy, 10, sizeof(myStruct), compare);
}

相关内容

  • 没有找到相关文章

最新更新