为什么我的程序(sort_them 函数)没有排序?

Why is my program (sort_them function) not sorting?

这是我的第一个 post。我四处搜索,但找不到任何有助于解决我的问题的具体问题。当我尝试取消引用指针或在函数中应用指针数学时,事情就是没有按预期进行。

例如:*(ptr + a) 应该在技术上将指针推进到我为 a 设置的任何值。相反,我只是收到一个错误 "request for member 'name' in something not a structure or union"。这没有意义,因为我已经声明并将我的结构指针传递给函数。如果我使用 :

"start_ptr->name.last + a" 风格,它编译但不排序任何东西。我知道这第二个版本在指针数学方面也不正确。

所以基本上,看起来错误的是编译,看起来正确的是错误的,实际上也没有对任何东西进行排序。

这是代码

#include <stdio.h>
#include <string.h> 
#include <ctype.h>

/* Structure definition */
/* -------------------- */

struct split
{
    char last[10];
    char first[10];
};

struct info
{
    struct  split name;
    char    address[20];
    char    city[15];
    char    state[3];
    long    zip;
    int     age;
    char    gender;
};  /* end struct info */


void sort_them(struct info *, char); // sort prototype

int main(void)  
{
/* Declare variables */
/* ----------------- */

struct info     people[] =
{

    {"Asimov", "Isaac", "15 Main St", "Worcestor", "MA", 01555, 23, 'M'},
    {"Smith", "Jane", "17 Make Peace", "Wallham", "ND", 10102, 28, 'F'},
    {"De Rippa", "Jack", "18 Able Way ", "Boston", "MA", 50503, 74, 'M'},
    {"Cobb", "Jim", "55 Elm St", "Ware", "MO", 61555, 65, 'M'},
    {"Kapone", "Al", "15 Morin Ave", "Idunno", "MN", 31333, 34, 'M'},
    {"Seigel", "Myron", "44 Wing Blvd West", "Sandwich", "WA", 02537,     21, 'M'},
    {"Thymes", "Mary", "88 Same Place", "Washington", "DC", 90555, 44, 'F'}

};

int             bad_sort;
int             num_people = sizeof(people) / sizeof(people[0]); //this will be the count of how many people there are!
char            sort_order;
char            big_name[22];





struct info *start_ptr = &people[0];
struct info *nums_ptr, *nums_end_ptr = &people[6];
char *big_ptr = &big_name[0];







printf ("\nWelcome to the People Structure Data Report Program\n");



do
{
        bad_sort = 1;

        printf ("\nEnter the sort order for the report: ");
        printf ("\n(N=Name, A=Age, S=State, Z=Zip code ' '=no sort)\n ");

/*               blank or return is allowed for no sorting of the data */

        sort_order = getchar();

        if(sort_order == '\n' || sort_order == ' ') break; 

        sort_order = toupper(sort_order);

        if ((sort_order == 'N') || (sort_order == 'A') || 
                    (sort_order == 'S') || (sort_order == 'Z'))
            bad_sort = 0;
        else
            printf("\nIncorrect Sort order selected, please re-enter:  ");

} while (bad_sort == 1);



switch (sort_order)
{
    case 'N':
        printf("Sort by Name  %i People.\n", num_people); 
        break;
    case 'A':
        printf("Sort by Age  %i People.\n", num_people); 
        break;
    case 'S':
        printf("Sort by State  %i People.\n", num_people); 
        break;
    case 'Z':
        printf("Sort by Zip  %i People.\n", num_people); 
        break;
    default:
        printf("No Sort selected  %i People.\n", num_people); 
        break;
}  /* end cases */

if(sort_order != ' ')
    sort_them (start_ptr, sort_order);


/* Print Report */
/* ------------ */

printf("\n\n                 The People Report\n\n");

printf ("\n\n%-20s%-20s %-15s %-5s %-6s %-3s %-6s", 
        "Name", "Address","City","State","Zip","Age","Gender");

printf ("%-20s%-20s %-15s %-5s %-6s %-3s %-6s\n", 
        "----", "------","----","-----","---","---","------");


for (nums_ptr = start_ptr; nums_ptr <= nums_end_ptr; nums_ptr++, start_ptr++)
{

    strcpy(big_ptr, start_ptr->name.last);
    strcat(big_ptr, ", ");
    strcat(big_ptr,start_ptr->name.first);



    printf ("%-20s%-20s %-15s  %-4s%.5ld    %-3i  %c\n", 
                                                                           big_ptr,start_ptr->address,start_ptr->city,start_ptr->state,start_ptr->zip,
            start_ptr->age,start_ptr->gender);      

} /* end for loop */



printf("\n\n");

return 0;

} /* end main */

void sort_them(struct info *start_ptr, char sort_by)
{
int a,b;
struct info temp;


for(a = 0; a < 6; a++)

    for(b = a + 1; b < 7; b++)
    {
        if(    (sort_by == 'N' && strcmp(start_ptr->name.last + a, start_ptr->name.last + b) > 0)
            || (sort_by == 'A' && start_ptr->age + a > start_ptr->age + b )
            || (sort_by == 'S' && strcmp(start_ptr->state + a, start_ptr->state + b) > 0)
            || (sort_by == 'Z' && (start_ptr->zip + a) > (start_ptr->zip + b) )  
          )


        {
            temp = *(start_ptr + a);
            *(start_ptr + a) = *(start_ptr + b);
            *(start_ptr + b) = temp;

        } /* end if  */


    } /* end inner for loop  */





};









/* end of sort them 

我认为下面一行

 strcmp( start_ptr->name.last + a, start_ptr->name.last + b) > 0  

应该像下面这样更改

strcmp( (start_ptr + a)->name.last,  
        (start_ptr + b)->name.last  
      ) > 0  

等等与其他比较。

首先没有理由不在 C:

中使用数组表示法
for(a = 0; a < 6; a++)
for(b = a + 1; b < 7; b++)
{
    if(    (sort_by == 'N' && strcmp(start_ptr[a].name.last, start_ptr[b].name.last) > 0)
        || (sort_by == 'A' && start_ptr[a].age > start_ptr[b].age )
        || (sort_by == 'S' && strcmp(start_ptr[a].state, start_ptr[b].state) > 0)
        || (sort_by == 'Z' && (start_ptr[a].zip) > (start_ptr[b].zip) )  
      )


    {
        temp = start_ptr[a];
        start_ptr[a] = start_ptr[b];
        start_ptr[b] = temp;

    } /* end if  */

表达式start_ptr->name.last + astart_ptr[0].name.last上加a,不是指针运算的"style"

指针算法可以通过 (ptr+a) 完成,其中 ptr 提前 a*sizeof(*ptr),如果 *ptr 指向有效对象。否则 *ptr 会产生段错误(例如 ptr=NULL)。然后你可以使用 *ptr 的类型:sizeof(type),在我们的例子中:sizeof(struct info).

基本上*(ptr+a)ptr[a]相同,(ptr+a)&ptr[a]

如果要使用->运算符,需要写成

(start_ptr+a)->name

例如。