我编写的代码接受命令行参数,并根据参数的ASCII值确定参数是否有序。以下是我目前拥有的:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int in_order(char *word){
int i = 1;
while(word[i] != ' '){
if(word[i] < word[i-1]){
return 0;
}
i++;
}
return 1;
}
int main(int argc, char *argv[]) {
if (argc < 2){
exit(0);
}
else{
char *word = argv[1];
if(in_order(strlwr(word)) == 1){
printf("In ordern");
}
else{
printf("Not in ordern");
}
}
return 0;
}
当我尝试使用C99标准编译此代码时,我收到以下警告和错误:
warning: implicit declaration of function 'strlwr' [-Wimplicit-function-declaration]
if(in_order(strlwr(word)) == 1){
^
warning: passing argument 1 of 'in_order' makes pointer from integer without a cast [enabled by default]
note: expected 'char *' but argument is of type 'int'
int in_order(char *word){
^
undefined reference to 'strlwr'
如何在不发生此错误的情况下使用strlwr函数?我是否应该注意其他错误?谢谢
strlwr
不是标准函数;仅在某些版本的CCD_ 2中发现。您可以在网上找到一个这样的string.h
,并将函数的代码复制到您的程序中。
你也可以自己实现:
char* strlwr (char* s) {
for (int i = 0; i < strlen(s); ++i)
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] += 'a' - 'A';
return s;
}
函数strlwr在cygwin string.h上可用,但它不是C99
参见/usr/include/string.h
#if __MISC_VISIBLE
char *strlwr (char *);
char *strupr (char *);
#endif
而不是
$ gcc -Wall -std=c99 prova.c -o prova
prova.c: In function ‘main’:
prova.c:25:21: warning: implicit declaration of function ‘strlwr’; did you mean ‘strstr’? [-Wimplicit-function-declaration]
25 | if(in_order(strlwr(word)) == 1){
只需降低-std=c99即可。
$ gcc -Wall prova.c -o prova
$ ./prova.exe ARD
Not in order
$ ./prova.exe ADR
In order