我正在尝试使用 strcmp() 对 C 中的字符串数组进行排序
I'm trying to use strcmp() to sort a string array in C
我有一个让我头疼的家庭作业挑战。问题是:
Build a program that uses an array of strings to store the following names:
* "Florida"
* "Oregon"
* "California"
* "Georgia"
Using the preceding array of strings, write your own sort() function to display > each state's name in alphabetical order using the strcmp() function.
这是给我带来问题的代码:
#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.
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
传递给字符串函数。
除了 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)
。
您还可以考虑将数组大小传递给该函数以避免幻数 3
和 4
:void sort(char *strStates[], int size)
.
您的 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()
你的排序函数也说它应该是 return 一个 int,但什么都没有 return。要么你 return 一些东西(比如 return 1
如果一切正常,或者 return 0
如果失败)或者你应该将它声明为 void
我有一个让我头疼的家庭作业挑战。问题是:
Build a program that uses an array of strings to store the following names:
* "Florida"
* "Oregon"
* "California"
* "Georgia"
Using the preceding array of strings, write your own sort() function to display > each state's name in alphabetical order using the strcmp() function.
这是给我带来问题的代码:
#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.
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
传递给字符串函数。
除了 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)
。
您还可以考虑将数组大小传递给该函数以避免幻数 3
和 4
:void sort(char *strStates[], int size)
.
您的 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()
你的排序函数也说它应该是 return 一个 int,但什么都没有 return。要么你 return 一些东西(比如 return 1
如果一切正常,或者 return 0
如果失败)或者你应该将它声明为 void