Home > other >  Encountering compiling issues with implicit declaration using the strlwr function for this code
Encountering compiling issues with implicit declaration using the strlwr function for this code

Time:03-10

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.

CodePudding user response:

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;
}

CodePudding user response:

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
  • Related