简体   繁体   中英

passing const *char to strlen() says i am passing unsigned long

char** strsep(const char* str)
{
    char** returnStrings = NULL;
    for (int i = 0; i < strlen(str); i++)
    {
        if (str[i] == ' ') returnStrings[sizeof(returnStrings)/sizeof(returnStrings[0])] = "";
        else returnStrings[sizeof(returnStrings)/sizeof(returnStrings[0])] += str[i];
    }
    return returnStrings;
}

I'm trying to make a function that can split a sentance(string) into an array of words(also strings), but it will not compile and says error: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)' [-Werror,-Wimplicit-function-declaration] . It says that I am entering an unsigned long as a parameter to strlen(), but I am not. i have #include <stdio.h> in my code.

You need to #include <string.h> to get the proper definition for strlen() .

Implicit declarations are a terrible mistake from years past.

Always compile with gcc -Wall -Werror .


Also, returnStrings is a NULL pointer and you never allocate memory, but try to dereference it.


Also, this is wrong:

returnStrings[sizeof(returnStrings)/sizeof(returnStrings[0])]

You probably googled for "C get size of array` and applied that pattern, but ignored the warning that said something like "This only works for arrays and not pointers. If you use it on a pointer, it will compile, but give incorrect results."


Also, this is wrong:

... += str[i]

You can't append a character to a "string" in C by using the += operator. That will manipulate the pointer, which is not at all what you intend.

Other than you're missing an declaration for <string.h> which seems to be your error.

Wouldn't strtok() fit what you want better? Since you mention you want to split a sentence into an array of words which are also strings?

Just to explain the error message:

It's not saying that you are passing unsigned long to the function. What it says is that you're implicitly declaring the function strlen , which was supposed to have type unsigned long (const char *) - that is, it's supposed to be a function which accepts one argument of type const char * and which returns unisgned long . According to the C standard, it's actually supposed to return size_t , so presumably size_t and unsigned long are the same type on your platform.

See Are prototypes required for all functions in C89, C90 or C99? if you really want to know more about what an implicit declaration is. To (over)simplify, if you use a function that hasn't been declared (which for a standard library function is done by including the appropriate standard header file), the compiler declares it for you; but, roughly speaking, it makes a guess according to a fixed set of rules as to the function's argument and return types, and that guess is usually wrong.

But it's enough to know that implicit declarations are bad and should always be eliminated from any code you write.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM