简体   繁体   English

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

[英]Sorting function using a string array - error in C

I created a 'sort by date' function that works correctly. 我创建了一个“按日期排序”功能,该功能可以正常工作。 However, when trying to create the 'sort by name' function, I assumed by that copying the 'sort by date' function and changing the variable yearTmp to nameTmp it would sort by name. 但是,当尝试创建“按名称排序”功能时,我假设复制“按日期排序”功能并将变量yearTmp更改为nameTmp时,它将按名称进行排序。 It does not. 它不是。 Program runs but I am seeing these errors in the compiler... 程序正在运行,但我在编译器中看到了这些错误...

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

Code I have so far... 我到目前为止的代码...

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

The hurricanes.txt file.... 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

You started down right path, but you can't treat c-string like integers. 您从正确的道路开始,但是您不能像对待整数一样对待c字符串。

A string in C is an array of characters terminated by the null terminator '\\0' character. C中的字符串是由空终止符'\\ 0'字符终止的字符数组。 In order to compare them, you have to check every element of the array. 为了比较它们,您必须检查数组的每个元素。 Luckily, a function exists that does that for you , namely strcmp() 幸运的是,存在一个可以为您完成此任务的函数,即strcmp()

What you pass into your function is an array of char *. 您传递给函数的是char *数组。 Each one of these indexes points to an array of characters representing your strings. 这些索引中的每个索引都指向一个代表字符串的字符数组。 To compare these index you can do the following. 要比较这些索引,您可以执行以下操作。

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

Its very important that both strings are null terminated or this function has undefined behavior. 两个字符串都以null终止或此函数具有未定义的行为非常重要。

To execute the swap part of the sort algorithm you swap the pointers. 要执行排序算法的交换部分,请交换指针。

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

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

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