简体   繁体   English

C 编程:Function 使用指针对数组进行排序

[英]C programming: Function using pointers to sort array

I'm having some problems with this function.我对这个 function 有一些问题。 Its main purpose is to sorts the array of classes(below) in descending order by the number of sections.其主要目的是按节数按降序对类数组(如下)进行排序。 Here is the array of classes:这是类的数组:

CIS_CLASSES *pCls, *pLast;
CIS_CLASSES  clsList[NUM_CLS] =
{
    {"CIS 35A", 2, {61, 63}},
    {"CIS 35B", 1, {62}},
    {"CIS 41A", 3, {1, 2, 61}},
    {"CIS 28",  1, {61}},
    {"CIS 22C", 4, {3, 4, 61, 63}},
    {"CIS 26B", 1, {61}},
    {"CIS 22B", 8, {1, 2, 3, 4, 6, 61, 62, 63}},
    {"CIS 29",  1, {61}},
    {"CIS 22A", 8, {1, 3, 5, 6, 7, 8, 61, 63}},
};

I've also defined NUM_CLS using struct:我还使用 struct 定义了 NUM_CLS:

#define NUM_CLS 9

typedef struct
{
    char  course[10];
    int   noSections;
    int   sections[16];
    int final;
} CIS_CLASSES;`

Here is the function:这是 function:

void sortDescend(CIS_CLASSES  list[], CIS_CLASSES *pLast);
{
    CIS_CLASSES *pCurr;
    CIS_CLASSES *pWalk;

    for (pCurr = list- 1; pCurr < pLast; pCurr++)
    {
        CIS_CLASSES temp = *pCurr;
        pWalk = pCurr - 1;
        while (pWalk >= 0 && temp.final > pLast->list[pWalk].final)
        {
            pLast->list[pWalk + 1] = pLast->list[pWalk];
            pWalk--;
        }
        pLast->list[pWalk + 1] = temp;
    }
  }
}

When I try to run this code I get the error message: no member named 'list' in 'CIS_CLASSES'.当我尝试运行此代码时,我收到错误消息:“CIS_CLASSES”中没有名为“list”的成员。 But I don't understand why?但我不明白为什么? and How can I make it run?我怎样才能让它运行? Please let me know if I'm missing anything!如果我遗漏了什么,请告诉我!

Looking at your structure I don't see an element list there:查看您的结构,我在那里看不到元素list

typedef struct
{
    char  course[10];
    int   noSections;
    int   sections[16];
    int final;
} CIS_CLASSES;

Therefore所以

pLast->list[pWalk].final

must fail.必须失败。

Thanks All: I updated my code to this :谢谢大家:我将我的代码更新为:

    void sortDescend(CIS_CLASSES  list[], CIS_CLASSES *pLast);

{ {

CIS_CLASSES *pCurr;
 CIS_CLASSES *pWalk;

for (pCurr = list+1; pCurr < pLast; pCurr++)
{
    CIS_CLASSES temp = *pCurr;
    pWalk = pCurr - 1;
    while (pWalk >= 0 && strcmp(temp.course, pWalk->course) > 0)
   {
         *(pWalk + 1) = *pWalk;
          pWalk--;
   }
   *(pWalk + 1)  = temp;
}

} } } }

Please try to do as the following.请尝试执行以下操作。

void sortDescend(CIS_CLASSES  pInput[], CIS_CLASSES **pOutput);
{
    // pOutput should be declared in your main function as the following
    // CIS_CLASSES **pOutput = (CIS_CLASSES **)malloc(NUM_CLS * sizeof(PVOID));

    // copy pointer arrays of CIS_CLASSES list elements
    for(int i=0; i<NUM_CLS; i++)
    {
        pOutput[i] = &pInput[i];
    }

    // bubble sort
    for (int i=0; i<NUM_CLS-1, i++)
    {
        for (int j=i+1; j<NUM_CLS; j++)
        {
            CIS_CLASSES temp = *pCurr;
            if(pOutput[i]->noSection < pOutput[j]->noSection)
            {
                pCurr = pOutput[i];
                pOutput[i] = pOutput[j];
                pOutput[j] = pCurr;
            }
        }
    }
    // You can access the descending sorted list item like pOutput[index]->course, ... 
}

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

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