在此代码中使用 strlwr 函数遇到隐式声明的编译问题

发布于 2025-01-12 23:02:39 字数 1189 浏览 1 评论 0原文

我正在编写接受命令行参数并根据参数的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2025-01-19 23:02:39

strlwr 不是标准函数;它仅存在于 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;
}

strlwr is not a standard function; it is only found in some versions of string.h. You can find one such string.h online and copy the function’s code into your program.

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;
}
↘人皮目录ツ 2025-01-19 23:02:39

函数 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

the function strlwr is available on cygwin string.h but it is NOT C99

see in /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.

$ gcc -Wall  prova.c -o prova

$ ./prova.exe ARD
Not in order

$ ./prova.exe ADR
In order
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文