【发布时间】:2025-12-12 01:00:01
【问题描述】:
我有一个让我头疼的家庭作业挑战。问题是:
构建一个使用字符串数组存储以下名称的程序:
*“佛罗里达”
*“俄勒冈”
*“加利福尼亚”
*“格鲁吉亚”
使用前面的字符串数组,编写您自己的 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.
【问题讨论】:
-
究竟有什么问题?
-
你有什么问题?没有人会为你做作业。
-
比如?编辑您的主要帖子,包括您尝试过的内容以及遇到的问题。
-
“我遇到了一系列错误”没有什么价值。发布错误以及与该错误相关的行。
-
现在看看你的帖子。它看起来像您的编译器输出吗?你能说出一条错误消息在哪里结束,另一条从哪里开始吗?