0
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.

person the human
  • 345
  • 4
  • 15
  • "Implicitly declaring" means you forgot to include the definition before using the function. Did you include the header file that defines `strlen`? Usually that's `#include ` – tadman Jun 17 '20 at 23:45
  • 5
    Add `#include ` to fix the warning/error about `strlen`. Without that a default prototype is used, which conflicts with the usage. But, if you do that, you'll get a conflict between _your_ `strsep` and the _real_ one. Consider renaming your function to something slightly different (e.g.) `my_strsep` – Craig Estey Jun 17 '20 at 23:46
  • 1
    Does this answer your question? [warning: implicit declaration of function](https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function) – Daniel Walker Jun 17 '20 at 23:47
  • Beyond the warning, this code exhibits undefined behavior; you're index-assigning to a `NULL` pointer. – ShadowRanger Jun 17 '20 at 23:49
  • 1
    Also, you are evaluating `strlen` on _each_ loop iteration--_very_ slow. You _could_ do: `int len = strlen(str);` above and use `len` instead. But, it's better to loop on `*str != 0;`, as in: `for (; *str != 0; ++str)` and change all `str[i]` to `*str`. Then, you don't need to call `strlen` at all [which passes over the string a second/unnecessary time]. – Craig Estey Jun 17 '20 at 23:54
  • Further, you still have UB because `returnStrings` is _uninitialized_ and is local/function scoped and goes out of scope when you return, so you _can't_ return it. The simplest fix is to make it `static` as in: `static char** returnStrings;`. But, it still points to invalid/null memory. – Craig Estey Jun 17 '20 at 23:57

3 Answers3

5

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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 2
    `-Wall` is not even *beginning* to enable "all" warnings. According to the manual it's "all" the **easy** warnings... start with something like `-Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-strings -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wshadow`, and then check the manual of your compiler version to find the *dozens* of other warnings you *could* use. Enable as many of them as you possibly can. The compiler is your friend. (And +1.) – DevSolar Jun 18 '20 at 00:14
  • i figured it out. i had another .c file that was included and had string.h, but the file where it was declared did not. i feel really silly for asking this question. my function was declared in the wrong file – person the human Jun 18 '20 at 01:09
0

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?

Alex
  • 76
  • 2
0

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.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82