简体   繁体   English

指向结构的指针数组

[英]Array of pointers that point to structures

My structure is this: 我的结构是这样的:

typedef struct celltag{
    char name[11];
    double time;
    struct celltag *next;
} celltype;

And structures of this type are saved in a array of linked lists: 这种类型的结构保存在一系列链接列表中:

typedef celltype **Hash;

and two structures X and Y are linked into the same list if, for some given function h(char x[]), h(X->name)=h(Y->name) . 如果对于某些给定函数h(char x[]), h(X->name)=h(Y->name) ,则两个结构X和Y链接到同一列表中。 What I'm trying to say is that these structures are sorted into the array by their 'names'. 我要说的是,这些结构按其“名称”排序到数组中。

Now, I have to sort the structures by their 'time' using array of pointers such that the first pointer in the array points to the structure with the smallest time, the second pointer to the second smallest etc. 现在,我必须使用指针数组按结构的“时间”对结构进行排序,以使数组中的第一个指针指向时间最小的结构,第二个指针指向时间最小的结构,依此类推。

How do I do that? 我怎么做? I'm still not very good with pointers so I don't know how to approach this at all. 我对指针仍然不太满意,所以我根本不知道该如何处理。 I tried looking at the similar questions already posted here but the pointers completely baffle me and I can't seem to make them work for my code. 我试图看一下已经在这里发布的类似问题,但是指针完全使我感到困惑,而且我似乎无法使它们适用于我的代码。

For example, if I have a function: 例如,如果我有一个功能:

void sort(Hash A, celltype *PArray[MAX]){
}

where *PArray[MAX] is my array of pointers and Hash A is where the structures are stored. 其中*PArray[MAX]是我的指针数组,而哈希A是存储结构的位置。 How do I write the rest of function? 如何编写其余功能? How to make a pointer point to a structure?? 如何使指针指向结构?

If you are still stuck, the overview of what you will need to do is iterate over your hash table (ie over each bucket and then over each node of the list contained in each bucket to make your array of pointers point to each entry in your hash table. It doesn't matter how you hash table is keyed, all you care about is making each used pointer in PArray point to a celltype . 如果您仍然被卡住,则需要做的概述是遍历哈希表(即遍历每个存储桶,然后遍历每个存储桶中列表的每个节点,以使指针数组指向您的每个条目)哈希表的键值无关紧要,您关心的只是使PArray每个使用的指针都指向一个celltype

I presume you know how to iterate over your hash table. 我想您知道如何迭代哈希表。 Just loop from 0 to ht_size (where htsize is the number of buckets, eg array elements, in your hash table). 只需从0ht_size循环(其中htsize是哈希表中存储区的数量,例如数组元素)。 You then simply declare a celltype *p pointer and iterate the list pointed to by the bucket entry (which can just be a single celltype* where the next pointer is NULL , or it could be any number based on the hash collision that resulted in more than one celltype* resolving to the bucket hashtable[x] ) 然后,您只需声明一个celltype *p指针并迭代存储桶条目指向的列表(它可以是单个celltype* ,其中next指针为NULL ,或者它可以是基于哈希冲突的任何数字,从而导致更多比一个单元格类型celltype*解析到存储桶hashtable[x]

Once you have filled PArray so that some number of pointers nptr now point to a celltype* in your hash table, all that remains is calling qsort to sort the pointers by the celltype->time member. 一旦填充了PArray以使一些指针nptr现在指向哈希表中的celltype* ,剩下的就是调用qsort来按celltype->time成员对指针进行排序。 The only trick is you must write the compare function to compare the adjacent celltype->time values. 唯一的技巧是必须编写compare函数来比较相邻的celltype->time值。

The qsort compare function has the prototype: qsort比较函数具有原型:

int compare (const void *ap, const void *bp);

(where a and b are pointers to adjacent elements being compared. I added the p just to denote them as pointers) (其中ab指向要比较的相邻元素的指针 。我添加了p只是为了将它们表示为指针)

What are we sorting? 我们要排序什么? An array of pointers to celltype . 指向 celltype 的指针数组 So each member of the array is already a pointer, and if each parameter is a pointer to your adjacent elements, then each parameter will represent a pointer to pointer to celltype . 因此,数组的每个成员已经是一个指针,并且如果每个参数都是指向您相邻元素的指针 ,则每个参数将代表一个指向 celltype 指针的指针

Once you have worked out what your compare pointers represent, it is just a matter of dereferencing the parameters to allow you to access the time member within each struct. 确定比较指针表示的内容后,只需取消引用参数的范围即可允许您访问每个结构中的time成员。 When each parameter is a pointer to pointer , you can cast the parameter to type * const * and dereference to provide you with a pointer to type that you can use to compare the time values, eg 当每个参数都是指向指针的指针时 ,可以将参数强制转换为type * const *并取消引用以提供一个指向该类型指针 ,可用于比较time值,例如

/* qsort compare for array of pointers to celltype */
int compare (const void *ap, const void *bp)
{
    celltype *a = *((celltype * const *)ap);
    celltype *b = *((celltype * const *)bp);

    return (a->time > b->time) - (a->time < b->time);
}

(for any numeric type, returning the result of the two conditionals (a > b) - (a < b) for an ascending sort, simply avoids potential overflow that could result if you return a - b , eg where a could be a large negative value and b a large positive value resulting in overflow) (对于任何数值类型,返回两个条件(a > b) - (a < b)进行升序排序,只是避免了如果返回a - b可能导致的潜在溢出,例如,其中a可能很大负值, b为大正值,导致溢出)

While you don't show how A is declared, you do show the typedef for Hash that when dereferenced would result in a pointer to celltype . 虽然没有显示A的声明方式,但确实显示了Hashtypedef ,当取消引用该类型定义时,将导致指向celltype的指针。 Using ht_size to represent the number of buckets in your hash table, your sort function would then look something like: 使用ht_size表示哈希表中的存储桶数, sort函数将如下所示:

void sort(Hash A, celltype *PArray[MAX]){

    size_t nptr = 0;

    for (size_t i = 0; i < ht_size; i++) {  /* loop over buckets */
        celltype *p = A[i];                 /* pointer to celltype */
        while (p) {                         /* iterate over all chained nodes */
            PArray[nptr++] = p;             /* assigning pointer to PArray */
            p = p->next
        }
    }

    qsort (PArray, ntpr, sizeof *PArray, compare);   /* call qsort */
}

That's it in a nutshell. 简而言之就是这样。 If your still having trouble wrapping your head around the qsort concept in the partial example above, a short full example should remove the confusion, eg 如果在上面的部分示例中仍然难以解决qsort概念的问题,则可以通过完整的简短示例消除混乱,例如

#include <stdio.h>
#include <stdlib.h>

typedef struct celltag{
    char name[11];
    double time;
    struct celltag *next;
} celltype;

int compare (const void *ap, const void *bp)
{
    celltype *a = *((celltype * const *)ap);
    celltype *b = *((celltype * const *)bp);

    return (a->time > b->time) - (a->time < b->time);
}

int main (void) {

    celltype    c1 = { .name = "three", .time = 127.21 },
                c2 = { .name = "one", .time = 127.1 },
                c3 = { .name = "two", .time = 127.19 },
            *pc[] = { &c1, &c2, &c3 };
    size_t n = sizeof pc / sizeof *pc;

    qsort (pc, n, sizeof *pc, compare);

    for (size_t i = 0; i < n; i++)
        printf ("%-5s  %6.2f\n", pc[i]->name, pc[i]->time);
}

Example Use/Output 使用/输出示例

$ ./bin/qsort_ptp_struct
one    127.10
two    127.19
three  127.21

Look things over and let me know if you have further questions. 仔细检查一下,如果您还有其他问题,请告诉我。

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

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