gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:16:07 25 4
gpt4 key购买 nike

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

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. 在您的函数中排序 -

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()不会返回任何东西,所以你又得到了自己UB.

注意 - 您收到的错误是由于将 char 传递给字符串函数所致。

关于c - 我正在尝试使用 strcmp() 对 C 中的字符串数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33219193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com