在此代码中使用 strlwr 函数遇到隐式声明的编译问题
我正在编写接受命令行参数并根据参数的 ASCII 值确定参数是否按顺序排列的代码。这是我现在所拥有的:
#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;
}
当我尝试使用 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 the strlwr function without getting this error indicates, and are there any othererror我应该注意什么?谢谢。
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. 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:
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? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strlwr
不是标准函数;它仅存在于string.h
的某些版本中。您可以在线找到一个这样的string.h
并将该函数的代码复制到您的程序中。您也可以自己实现:
strlwr
is not a standard function; it is only found in some versions ofstring.h
. You can find one suchstring.h
online and copy the function’s code into your program.You could also implement it yourself:
函数 strlwr 在 cygwin string.h 上可用,但它不是 C99,
请参见
/usr/include/string.h
而不是
直接删除 -std=c99。
the function strlwr is available on cygwin string.h but it is NOT C99
see in
/usr/include/string.h
instead of
just drop the -std=c99.