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

时间:2016-04-15 01:56:41

标签: c arrays function pointers c-strings

我创建了一个“按日期排序”功能,可以正常工作。但是,在尝试创建“按名称排序”功能时,我假设通过复制“按日期排序”功能并将变量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

1 个答案:

答案 0 :(得分:1)

你是从正确的路径开始的,但你不能像对整数那样对待c-string。

C中的字符串是由空终止符'\ 0'字符终止的字符数组。为了比较它们,您必须检查数组的每个元素。幸运的是,存在一个为您执行此操作的函数,即strcmp()

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

    line_instance.draw()

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

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

// 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)