简体   繁体   English

使用结构数组在C中实现排序算法时遇到问题

[英]Problem implementing sorting algorithm in C with an array of structs

Well here is my little problem, first my code: 好吧,这是我的小问题,首先是我的代码:


struct alumn {
    char name[100];
    char lastname[100];
    int par;
    int nota;
};

typedef struct alumn alumn;

int bubble(alumn **arr, int length) { int i,j; alumn *temp;

for (i=0; i<=length-2; i++) {
    for (j=i+1; j<=length-1;j++) {
        if ((*arr)[i].nota > (*arr)[j].nota) {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
}

}

int main(int argc, char **argv) { alumn *alumns;

... here goes some other code ...

bubble(&alumns,totalAlumns);

return 0;

}

My problem is that this algorith is not sorting anything. 我的问题是该算法未对任何内容进行排序。 I'm having a hard time doing the swap, i tried everything but nothing works :( . Any help??? 我在进行交换时遇到了困难,我尝试了一切,但没有任何效果:(。任何帮助???

Apparently you're confusing an array of alumn structs with an array of pointers to alumm struct . 显然,您正在将铝结构 的数组与指向铝结构的指针的数组混淆

The Bubble logic expexts the array of pointers, whereby the main function seems to call it with an array of structs. Bubble逻辑扩展了指针数组,由此主函数似乎使用结构数组对其进行调用。

Because of the size of the alumn struct, it is probably more efficient to perform the bubble sort on pointers, since each swap will require much less moving around of data (3 copies of one pointer a few bytes each, vs. 3 copies of alumn struct, 200+ bytes each!). 由于校友结构的大小,对指针执行冒泡排序可能会更有效,因为每次交换都需要更少的数据移动(一个指针的3个副本每个字节几个字节,而校友的3个副本struct,每个200+字节!)。

I suggest you modify the logic (not shown in snippet of question) of the main() function to introduce such an array of pointers to the actual alumn structs. 我建议您修改main()函数的逻辑(问题片段中未显示),以将这样的指针数组引入实际的校友结构。 (Of course this array of pointers won't spare you from also allocating the structs themselves, en block (in an array of structs) or individually. (当然,该指针数组不会使您自己也分配结构体,en块(在结构体数组中)或单独分配结构体。


Upon your insistence I'm hinting here how main could look like to produce an array of pointer usable by bubble (bubble stays unchanged). 在您的坚持下,我在这里提示main如何看起来像产生一个可用于bubble的指针数组(bubble保持不变)。
BTW, I declare alumns as alumn *alumns[] which shows the intent use more readily. 顺便说一句,我将alumn *alumns[]声明为alumn *alumns[] ,它更容易显示其意图。 this is the same thing as alumn **alumns . 这和alumn **alumns

int main(int argc, char **argv)
{
    alumn *alumns[];   // changed to array of pointers [to alumn structs] 
                       // was pointer to alumn struct, likely to be used as an array thereof

    int MaxNbOfAlumns = some_limit;
    alumns = malloc(sizeof(*alumn) * MaxNbOfAlumns);

    // Load the alumn records (from file or whereever)
    // pseudo code:
    // int i_alumns = 0;  // points to the next free slot in alumns array
    // for each record (in file or whereever...)
    //     alumms[i_alums] = malloc(sizeof(struct alumn));
    //     strcpy(alumms[i_alums]->lastname,  whatever_data);
    //     strcpy(alumms[i_alums]->name,  whatever_otherdata);
    //     alumms[i_alums]->par = some_int_data;
    //     alumms[i_alums]->nota = some_other_int_data;
    //     i_alums++;

... here goes some other code ...

  bubble(alumns, totalAlumns);   // alumns now being an array can be passed as is.

  return 0;
}

Alternatively if you wish to keep the original alumns variable as previously, all that may be needed is something like that, just before the call to bubble() 另外,如果您希望像以前一样保留原始的校友变量,那么在调用bubble()之前,可能需要做的就是这样

  int i;
  alumn *ap_alumns[];   // new variable
  ap_alumns = malloc(sizeof(*alumn) * totalAlumns);
  for (i = 0; i < totalAlumns; i++)
      ap_alums[i] = &alumns[i];
  bubble(ap_alumns, totalAlumns);  

One thing that should be stressed is that regardless of its genesis, the array passed to bubble() gets sorted ok, but to use it you need to dereference the individual pointers. 应该强调的一件事是,不管其起源如何,传递给bubble()的数组都可以正确排序,但是要使用它,您需要取消引用各个指针。
Whereby with the old array you intended to use as in say alumns[123].lastname , you'll now need alumns[123]->lastname (or ap_alumns[123]->lastname if you use the second version). 因此与旧阵列您打算在说使用alumns[123].lastname ,你现在需要alumns[123]->lastname (或ap_alumns[123]->lastname ,如果你使用第二版)。

Your code does not work because you have an array of structs instead of an array of pointers. 您的代码不起作用,因为您有一个结构数组而不是指针数组。

When you try to swap two structs, the = operator does not know what to do. 当您尝试交换两个结构时,=运算符不知道该怎么做。 You have to copy the fields of the struct one by one for that to work. 您必须一一复制该结构的字段才能正常工作。

If you had an array of pointers to instances of the alumn struct instead. 如果您有一个指针数组来代替明矾结构的实例。 THen the code would work, since you are assigning pointers. 这样代码就可以了,因为您正在分配指针。 A pointer is basically a number and = knows how to copy a number. 指针基本上是一个数字,并且=知道如何复制数字。

Your code is written as if you have an array of pointers, but have left out an important part of your code (ie, ... here goes some other code ... ) so we can't see how you've set up the thing to be sorted. 您的代码被编写为好像有一个指针数组,但是遗漏了代码的重要部分(即... here goes some other code ... ),所以我们看不到您如何进行设置要分类的东西。 If you have an array of pointers, this line of code: 如果您有一个指针数组,则此行代码:

if ((*arr)[i].nota > (*arr)[j].nota) {

should be: 应该:

if (arr[i]->nota > arr[j]->nota) {

The reason is, (*arr) gets the first pointer in the list, then (*arr)[i] gets the i'th item in memory after that pointer (meaningless since each pointer should point to one item). 原因是,(* arr)获取列表中的第一个指针,然后(* arr)[i]获取该指针之后的内存中的第i个项目(无意义,因为每个指针都应指向一个项目)。 The 2nd syntax goes to the i'th pointer in the pointer array, and dereferences it to get that value. 第二种语法转到指针数组中的第i个指针,并对其取消引用以获取该值。

Maybe this illustration will help: 也许此插图将有助于:

arr points to an array of two pointers, each pointing to a struct.
(*arr)[1].nota refers to an item in ??????.
arr[1]->nota refers to an item in struct 2.

       +---+                   +----------+
arr -> | * | ----------------> | struct 1 |
       +---+    +----------+   +----------+
       | * | -> | struct 2 |   :  ??????  :
       +---+    +----------+   +..........+

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

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