我如何在C中使用一系列指针?
基本上,我有一系列的字符串,我需要检查它们是否按照词法顺序排列(对上或较低的情况不敏感,这意味着它是哪一个无关紧要)。为此,我采用了已经创建的strCMP功能并重新安排了它,以使其上层或较低的情况都没有关系。但是,当输入str_compare时,它不会输入WARE循环,而只是退出功能。 有人知道为什么吗? 这是代码:
int main(){
...
char * banned_words[N];
...
if (!are_sorted(banned_words, n)) {
printf("Words are not sorted correctly!\n");
free_strings(banned_words, n);
return ERROR;
}
...
}
bool are_sorted(char * strings[], int n) {
int length = n;
int result = 0;
for (int i = 0; i < length - 1; i++) {
result += str_compare(strings[i], strings[i + 1]);
}
if (result > 0)
return false;
else
return true;
}
int str_compare(char **str1, char **str2){
while (str1 && str2) {
if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){
**str1++;
**str2++;}
else if (str2 >= 'A' && str2<='Z' && (str2 + 'a' - 'A') == str1){
**str1++;
**str2++;}
else if (str1 == *str2){
**str1++;
**str2++;}
else break;
}
if (str1 >= 'A' && str1<='Z')
return (str1 +'a' - 'A') - str2;
else if (str2 >= 'A' && str2<='Z')
return str1 - (str2 +'a'-'A');
else return str1 - str2;
}
Basically i have an array of strings and i need to check if they are arranged in lexographic order (case insensitive of upper or lower case, meaning it doesn't matter which one it is). For that I took the already created strcmp function and re-arranged it so that it won't matter if it's upper or lower case. But when entering str_compare, it won't enter the while loop and just exits the function.
Does anyone know why?
Here is the code:
int main(){
...
char * banned_words[N];
...
if (!are_sorted(banned_words, n)) {
printf("Words are not sorted correctly!\n");
free_strings(banned_words, n);
return ERROR;
}
...
}
bool are_sorted(char * strings[], int n) {
int length = n;
int result = 0;
for (int i = 0; i < length - 1; i++) {
result += str_compare(strings[i], strings[i + 1]);
}
if (result > 0)
return false;
else
return true;
}
int str_compare(char **str1, char **str2){
while (str1 && str2) {
if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){
**str1++;
**str2++;}
else if (str2 >= 'A' && str2<='Z' && (str2 + 'a' - 'A') == str1){
**str1++;
**str2++;}
else if (str1 == *str2){
**str1++;
**str2++;}
else break;
}
if (str1 >= 'A' && str1<='Z')
return (str1 +'a' - 'A') - str2;
else if (str2 >= 'A' && str2<='Z')
return str1 - (str2 +'a'-'A');
else return str1 - str2;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用类型
char *
的表达式调用函数str_compare
,而函数参数具有类型
char **
。这是循环
调用不确定的行为,因为循环的条件将至少一个指针变成零指针时,循环的状况会评估为false。
同样在if语句中的功能条件下,例如
或
没有意义。比较操作数具有不同的类型。
此外,此代码段中的返回语句
也可以产生不可预测的结果。
而且该函数中的代码片段
are_sorted
实际上并不确定是否对字符串的数组进行排序。例如,一对字符串的比较可以产生
-1
,而另一对字符串的比较可以产生1
,结果将等于0 <
0 < /code>虽然数组未排序。
函数
str_compare
可以按照以下方式定义以下方式。程序输出是
如果您不使用标准C函数
TOLOWER
,那么函数str_compare
可以以下方式查看函数
aws_sorted
然后可以定义以下方式You call the function
str_compare
with expressions of the typechar *
While function parameters have the type
char **
.This while loop
invokes undefined behavior because the condition of the loop evaluates to false when at least one pointer will become a null pointer.
Also within the function conditions in the if statements like for example
or
do not make a sense. Compared operands have different types.
Also the return statements in this code snippet
can produce unpredictable results.
And this code snippet within the function
are_sorted
actually does not determine whether the array of strings is sorted. For example the comparison of one pair of strings can yield
-1
while the comparison of other pair of strings can yield1
and the result will be equal to0
though the array is not sorted.The function
str_compare
can be defined the following way as shown in the demonstrative program below.The program output is
If you may not use the standard C function
tolower
then the functionstr_compare
can look the following wayAs for the function
are_sorted
then it can be defined for example the following way