[英]Compiling and Linking pure C and CUDA code [warning: implicit declaration of function]
[英]Encountering compiling issues with implicit declaration using the strlwr function for this code
我正在编写接受命令行参数并根据参数的 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'
如何在不发生此错误的情况下使用 strlwr function,还有其他我应该注意的错误吗? 谢谢。
strlwr
不是标准的 function; 它只存在于某些版本的string.h
中。 您可以在线找到一个这样的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;
}
function 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.