我正在尝试使用strcmp()在C中对字符串数组进行排序

时间:2015-10-19 16:12:53

标签: c arrays string strcmp

我有一个让我头痛的家庭作业挑战。问题是:

  

构建一个使用字符串数组存储以下名称的程序:
  *“佛罗里达”
  *“俄勒冈”
  *“加利福尼亚”
  *“格鲁吉亚”
  使用前面的字符串数组,编写自己的sort()函数来显示>使用strcmp()函数按字母顺序排列每个州的名称。

这是我的代码,它给了我一些问题:

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

//function prototype
int sort(char *, char *);

int main() 
{
    char *strStates[] = {"Florida", "Oregon", "California", "Georgia"};
    char *strSorted[] = {0};
    int x;

    printf("\nThe list of states before being sorted in alphabetical order: ");

    for (x = 0; x < 4; x++) 
    {
        printf("\n%s", strStates[x]);
    }

    sort(strStates[x], strSorted[x]);

    printf("\nThe list of states sorted alphabetically are: ");

    for (x = 0; x < 4; x++) 
    {
        printf("\n%s", strStates[x]);
    }

    return 0;

}//end main

//function definition
int sort(char *string1, char *string2)
{
    int x;
    int y;

    for (x = 0; x < 3; x++) 
    {
        for (y = 1; y < 4; y++) 
        {
            if ((strcmp(string1[x], string1[y])) > 0) 
            {
                strcpy(string2, string1[x]);
                strcpy(string1[x], string1[y]);
                strcpy(string[y], string2);
            }//end if

       }//end inner for loop

    }//end outer for loop

}//end sort()

编译时我遇到了一系列错误:

Chapter_8_Challenge_3.c:45:16: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Wint-conversion]
                    if ((strcmp(string1[x], string1[y])) > 0) 
                                ^~~~~~~~~~
                                &  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/string.h:77:25: note: passing argument to parameter here
int      strcmp(const char *, const char *);
                        ^  
Chapter_8_Challenge_3.c:45:28: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Wint-conversion]
                    if ((strcmp(string1[x], string1[y])) > 0) 
                                            ^~~~~~~~~~
                                            &  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/string.h:77:39: note: passing argument to parameter here
int      strcmp(const char *, const char *);
                                      ^  
Chapter_8_Challenge_3.c:47:21: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Wint-conversion]
                            strcpy(string2, string1[x]);
                                            ^~~~~~~~~~
                                            &  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_string.h:83:33: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                            ^  
Chapter_8_Challenge_3.c:48:12: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'const void *' [-Wint-conversion]
                            strcpy(string1[x], string1[y]);
                                   ^~~~~~~~~~  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_string.h:83:53: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                                                ^  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_common.h:39:54: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)
                                                 ^  
Chapter_8_Challenge_3.c:48:12: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with & [-Wint-conversion]
                            strcpy(string1[x], string1[y]);
                                   ^~~~~~~~~~
                                   &  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_string.h:83:27: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                      ^  
Chapter_8_Challenge_3.c:48:24: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Wint-conversion]
                            strcpy(string1[x], string1[y]);
                                               ^~~~~~~~~~
                                               &  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_string.h:83:33: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                            ^  
Chapter_8_Challenge_3.c:49:12: error: use of undeclared identifier 'string'; did you mean 'string1'?
                            strcpy(string[y], string2);
                                   ^~~~~~
                                   string1  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_string.h:83:27: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                      ^  
Chapter_8_Challenge_3.c:36:16: note: 'string1' declared here
int sort(char *string1, char *string2)
           ^  
Chapter_8_Challenge_3.c:49:12: error: use of undeclared identifier 'string'
                            strcpy(string[y], string2);
                                   ^  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_string.h:83:53: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                                                ^  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/secure/_common.h:39:54: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIFY_LEVEL > 1 ? 1 : 0)  
                                                 ^
6 warnings and 2 errors generated.

3 个答案:

答案 0 :(得分:1)

1。在您的函数sort -

if((strcmp(string1[x],string1[y]))>0) //string1[x] is of type char-Wrong argument to strcmp 
        {
            strcpy(string2, string1[x]);          // same problem as aboce for all 
            strcpy(string1[x], string1[y]);
            strcpy(string[y], string2);
        }//end if 

string1已为char *string1[x]的类型为char。所以这会调用未定义的行为

2。main -

 for (x = 0; x < 4; x++) 
{
    printf("\n%s", strStates[x]);
}

sort(strStates[x], strSorted[x]);

也许您打算将char指针数组(strStates)作为第一个参数传递,并将(strSorted)作为第二个参数传递。那么sort中的上述问题也将得到解决。

3。最后,您的int sort()功能不会return任何内容,所以您再次获得 UB

注意 - 您收到的错误是因为将char传递给字符串函数。

答案 1 :(得分:0)

除了sort函数参数的错误之外,您还应该考虑使用strcpy。您不能将字符串文字复制到另一个字符串文字中,因为它们是只读的。

你真的需要复制字符串吗?您有输入字符串指针的数组。您只能在不触及实际字符串的情况下对这些指针进行排序。

你可以传递那个指针数组并对它们进行排序:

void sort(char *strStates[])
{
    int x, y;

    for (x = 0; x < 3; x++) 
    {
        for (y = x + 1; y < 4; y++)
        {
            if ((strcmp(strStates[x], strStates[y])) > 0) 
            {
                char *tmp = strStates[x];
                strStates[x] = strStates[y];
                strStates[y] = tmp;
            }//end if
       }//end inner for loop
    }//end outer for loop
}//end sort()

可以将其称为sort(strStates)

您还可以考虑将数组大小传递到该函数中,以避免幻数34void sort(char *strStates[], int size)

答案 2 :(得分:0)

您的sort函数收到2个字符串作为参数,因此if ((strcmp(string1[x], string1[y])) > 0)正在尝试将两个字符与strcmp进行比较。你的排序功能应该更像这样:

void sort(char **unsortedStrings){
    int x;
    int y;
    for (x = 0; x < 3; x++) 
    {
        for (y = x+1; y < 4; y++) 
        {
            if ((strcmp(unsortedStrings[x], unsortedStrings[y])) > 0) 
            {
                // There is no need to use strcpy here, just re-order the pointers
                char *tmp = unsortedStrings[x];
                unsortedStrings[x] = unsortedStrings[y];
                unsortedStrings[y] = tmp;
            }//end if

      }//end inner for loop

   }//end outer for loop
}//end sort()

此外,您的排序函数表示它应该返回一个int,但没有返回任何内容。您要么返回一些内容(如果一切正常则为return 1,或者如果失败则为return 0),或者您应将其声明为void