简体   繁体   English

指向结构和功能的指针

[英]Pointers to structures and functions

I have a simple structure called entry defined which contains name and age. 我有一个简单的结构,称为条目定义,其中包含名称和年龄。 Given an array of these structures, I want to sort the array based on age. 给定这些结构的数组,我想根据年龄对数组进行排序。

Below is my attempt at applying this, at the moment I can't even get this to compile. 下面是我尝试应用此代码的尝试,目前我什至无法编译它。 I think my pointer logic is incorrect in both the if statement comparison and the subsequent swapping of the pointers. 我认为在if语句比较和随后的指针交换中,我的指针逻辑都是错误的。 I've tried various ways to do the same thing, but I'm not getting anywhere. 我已经尝试了多种方法来完成相同的操作,但是却一无所获。 I'm pretty new to C, and I'm still trying to get my head around pointers, so it's probably something basic I'm misunderstanding. 我对C还是很陌生,但我仍在努力弄清楚指针,所以这可能是我误会的一些基本知识。 Can anybody please explain what I'm doing wrong below? 有人可以在下面解释我在做什么错吗?

Any help would be greatly appreciated. 任何帮助将不胜感激。

#include <stdio.h>

struct entry {
    char name[15];
    int age;
};

void entrySort( struct entry *dict);

void entrySort( struct entry *dict){
    int i,j;   // counters
    int ct = 4;
    struct entry *tmp;  // temporary holder

    for( i = 0; i < ct; i++){
        for( j = 0; j < ct; j++ ){
            if ((*dict[i].age) > (*dict[j].age)){
            tmp = (dict + i);
            (dict+i) = (dict+j);
            (dict+j) = tmp;

        }
    }
}

int main (void){
    int ct = 4, i;
    struct entry reg[4] =
       {{ "John", 24 },
        { "Alan", 18 },
        { "Jim", 40 },
        { "Sarah",32 }};

     entrySort(reg);

    for( i = 0; i < ct; i++)
        printf("name: %s. Age: %d\n", reg[i].name, reg[i].age);

   return 0;
}

You pass an array of struct entry objects as a pointer: struct entry *dict , but you are treating it as it would be an array of pointers to struct entry objects: (*dict[i]).age . 您将struct entry对象的数组作为指针传递: struct entry *dict ,但是您将其视为将指向struct entry对象的指针的数组: (*dict[i]).age

(dict+i) is still just a pointer pointing to the memory where i+1 . (dict+i)仍然只是指向内存中i+1的指针。 element is stored, ie &dict[i] . 元素被存储,即&dict[i] To actually access this element at index i , you need to use dereference operator : *(dict + i) , which is equal to dict[i] . 要实际访问索引i处的该元素,您需要使用解引用运算符*(dict + i) ,它等于dict[i]

And also note that your swapping of elements at i and j is wrong. 还要注意,您在ij处交换元素是错误的。 "Temporary holder" tmp should be an object that will temporarily hold data, not just a pointer to memory, that you are going to rewrite, thus declare it as struct entry tmp; “临时持有人” tmp应该是一个将临时保存要重写的数据的对象,而不仅仅是指向内存的指针,因此将其声明为struct entry tmp; :

struct entry tmp;

for( i = 0; i < ct; i++) {
    for( j = 0; j < ct; j++ ) {
        if ((dict[i].age) > (dict[j].age)) {
            tmp = dict[i];
            dict[i] = dict[j];
            dict[j] = tmp;
        }
    }
}

By the way in the code you have posted, the ending curly brace ( } ) of your if is missing. 顺便说一下,在您发布的代码中, if的结尾大括号( } )丢失了。

Try: 尝试:

#include <stdio.h>

struct entry {
    char name[15];
    int age;
};

void entrySort( struct entry *dict, int);

void entrySort( struct entry *dict, int ct){
    int i,j;   // counters
    /* int ct = 4; */
    struct entry tmp;  // temporary holder

    for( i = 0; i < ct; i++){
        for( j = 0; j < ct; j++ ){
            if ((dict[i].age) > (dict[j].age)){  /* no *  */
            tmp = *(dict + i);
            *(dict+i) = *(dict+j);
            *(dict+j) = tmp;

        }
    }
}

int main (void){
    int ct = 4, i;
    struct entry reg[4] =
       {{ "John", 24 },
        { "Alan", 18 },
        { "Jim", 40 },
        { "Sarah",32 }};

     entrySort(reg, ct);

    for( i = 0; i < ct; i++)
        printf("name: %s. Age: %d\n", reg[i].name, reg[i].age);

   return 0;
}

For completeness, here's how you'd do it with qsort : 为了完整qsort这是使用qsort

#include <stdlib.h>
int sort_entry(const void *va, const void *vb) {
    const struct entry *a = va;
    const struct entry *b = vb;
    if(a->age < b->age) return -1;
    else if(a->age == b->age) return 0;
    return 1;
}

...

qsort(reg, ct, sizeof(struct entry), sort_entry);

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

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