使用字符串数组的排序函数 - C 中的错误

Sorting function using a string array - error in C

我创建了一个可以正常工作的 'sort by date' 函数。但是,当尝试创建 'sort by name' 函数时,我假设通过复制 'sort by date' 函数并将变量 yearTmp 更改为 nameTmp 它将按名称排序。它不是。程序运行但我在编译器中看到这些错误...

[Warning] assignment makes integer from pointer without a cast 
[Warning] assignment makes pointer from integer without a cast

我目前拥有的代码...

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

#define MAX 30

void sortByDate( int year[], char *name[], char *states[], int count);
void sortByName(int year[], char *name[], char *states[], int count);

int main()
{
     int year[MAX]; 
     int i, a;
     int count = 0;
     int choice;
     char *name[MAX],
          *states[MAX];
     char b[MAX], c[MAX];

     FILE *inp = fopen("hurricanes.txt","r");               /* defining file input    */

     for(i=0;i<MAX;i++)
     {
         if( feof(inp) )
        {
            break;
        } 

        fscanf(inp, "%d %s %29[^\n]", &a, b, c);
        year[i]=a;
        name[i] = strdup(b);
        states[i] = strdup(c);
        ++count; 

        printf("%d %s %s\n", year[i], name[i], states[i]);
     }

     printf("Press 0 to sort by date or 1 to sort by name: ");
     scanf("%d", &choice);  
     if (choice == 0)
     {
         sortByDate(year, name, states, count); 
     }
     else if ( choice == 1)
     {
          sortByName(year, name, states, count); 
     }

     getch();
     return 0;
}

void sortByDate( int year[], char *name[], char *states[], int count )
{
     int d = 0;
     int c = 0;

     int yearTmp;
     int order[count];
     int tmp = 0;

     FILE *outp = fopen("report.txt","w");                 /* defining file output   */

     for (c = 0; c < count; ++c)
     {
         order[c] = c; 
     } 

     for (c = 0 ; c < ( count - 1 ); c++)
     {
          for (d = 0 ; d < count - c - 1; d++)
          {
               if (year[d] > year[d+1])
               {
                    yearTmp = year[d];
                    year[d] = year[d+1]; 
                    year[d+1] = yearTmp; 

                    tmp = order[d];
                    order[d] = order[d+1];
                    order[d+1] = tmp;   
              }
          }
     }

     for (c = 0; c < count; ++c)
     {
          printf("%d %-10s %s\n",  year[c], name[order[c]], states[order[c]]); 
     } 
}

void sortByName(int year[], char *name[], char *states[], int count)
{
     int d = 0;
     int c = 0;

     char nameTmp;
     int order[count];
     int tmp = 0;

     FILE *outp = fopen("report.txt","w");                 /* defining file output   */

     for (c = 0; c < count; ++c)
     {
         order[c] = c; 
     } 

     for (c = 0 ; c < ( count - 1 ); c++)
     {
          for (d = 0 ; d < count - c - 1; d++)
          {
               if (name[d] > name[d+1])
               {
                    nameTmp = name[d];
                    name[d] = name[d+1]; 
                    name[d+1] = nameTmp; 

                    tmp = order[d];
                    order[d] = order[d+1];
                    order[d+1] = tmp;   
              }
          }
     }

     for (c = 0; c < count; ++c)
     {
          printf("%d %-10s %s\n",  year[order[c]], name[c], states[order[c]]); 
     } 
}

hurricanes.txt 文件....

1960 Donna FL, NC
1969 Camille MS
1972 Agnes FL
1983 Alicia TX
1989 Hugo SC,NC
2005 Katrina FL, LA, MS
2005 Rita TX, LA
2005 Wilma FL
2008 Ike TX
2009 Ida MS
2011 Irene NC, NJ, MA, VT
2012 Isaac LA
1992 Andrew FL, LA
1995 Opal FL, AL
1999 Floyd NC
2003 Isabel NC, VA
2004 Charley FL, SC, NC
2004 Frances FL
2004 Ivan AL
2004 Jeanne FL

您的起点是正确的,但您不能像对待整数一样对待 c 字符串。

C 中的字符串是由空终止符“\0”字符终止的字符数组。为了比较它们,您必须检查数组的每个元素。幸运的是,存在一个函数可以为您做到这一点,即 strcmp()

你传入函数的是一个char *数组。这些索引中的每一个都指向代表您的字符串的字符数组。要比较这些索引,您可以执行以下操作。

// Returns 0 if the are equal
// Returns something > 0 if pName[d] is lexicographically greater than pName[d+1]
// Returns something < 0 if pName[d] is lexicographically less than pName[d+1]
if(strcmp(pName[0], pName[d+1]) > 0)

两个字符串都以 null 终止或此函数具有未定义的行为非常重要。

要执行排序算法的交换部分,您需要交换指针。

char * pTemp = pName[d];
pName[d] = pName[d+1];
pName[d+1] = pTemp;