[英]Encountering compiling issues with implicit declaration using the strlwr function for this code
I am writing code that accepts a command-line argument and determines whether or not the argument is in order based on the ASCII values of the argument.我正在编写接受命令行参数并根据参数的 ASCII 值确定参数是否有序的代码。 Here is what I have as of now:这是我现在所拥有的:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int in_order(char *word){
int i = 1;
while(word[i] != '\0'){
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 order\n");
}
else{
printf("Not in order\n");
}
}
return 0;
}
When I try to compile this code with the C99 standard, I receive the following warnings and errors:当我尝试使用 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'
How can I make use of the strlwr function without having this error occur, and are there any other mistakes I should be aware of?如何在不发生此错误的情况下使用 strlwr function,还有其他我应该注意的错误吗? Thanks.谢谢。
strlwr
is not a standard function; strlwr
不是标准的 function; it is only found in some versions of string.h
.它只存在于某些版本的string.h
中。 You can find one such string.h
online and copy the function's code into your program.您可以在线找到一个这样的string.h
并将该函数的代码复制到您的程序中。
You could also implement it yourself:你也可以自己实现:
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;
}
the function strlwr is available on cygwin string.h but it is NOT C99 function strlwr 在 cygwin string.h 上可用,但它不是 C99
see in /usr/include/string.h
见/usr/include/string.h
#if __MISC_VISIBLE
char *strlwr (char *);
char *strupr (char *);
#endif
instead of代替
$ 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){
just drop the -std=c99.只需删除 -std=c99。
$ gcc -Wall prova.c -o prova
$ ./prova.exe ARD
Not in order
$ ./prova.exe ADR
In order
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.