【问题标题】:Pointers to structures and functions指向结构和函数的指针
【发布时间】:2013-02-17 00:04:14
【问题描述】:

我有一个简单的结构,称为条目定义,其中包含姓名和年龄。给定这些结构的数组,我想根据年龄对数组进行排序。

以下是我尝试应用它的尝试,目前我什至无法编译它。我认为我的指针逻辑在 if 语句比较和随后的指针交换中都不正确。我尝试了各种方法来做同样的事情,但我没有得到任何地方。我对 C 很陌生,我仍在努力理解指针,所以这可能是我误解的基本内容。谁能在下面解释我做错了什么?

任何帮助将不胜感激。

#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;
}

【问题讨论】:

  • 你能用qsort吗?会容易很多。
  • 主要目标不是排序本身,更多的是弄清楚如何正确使用指针。

标签: c function pointers structure


【解决方案1】:

您将 struct entry 对象数组作为指针传递:struct entry *dict,但您将其视为指向 struct entry 对象的指针数组:(*dict[i]).age

(dict+i) 仍然只是一个指向i+1 所在内存的指针。元素被存储,即&amp;dict[i]。要实际访问索引i 处的此元素,您需要使用dereference operator*(dict + i),它等于dict[i]

还要注意,您在 ij 处交换元素是错误的。 “临时持有者”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;
        }
    }
}

顺便说一句,在您发布的代码中,您的 if 的结尾大括号 (}) 丢失了。

【讨论】:

    【解决方案2】:

    试试:

    #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;
    }
    

    【讨论】:

      【解决方案3】:

      为了完整起见,以下是使用 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-15
        • 2023-04-04
        • 2021-01-26
        • 1970-01-01
        • 2021-11-24
        相关资源
        最近更新 更多