1

First of all I have to said that my brain is burned because of this question. I need to write a function that checks whether the 3rd parameter is a interleaved of 1st and 2nd parameter. I wrote it but when I run it, it gives me this error. "warning: implicit declaration of function ‘interleaved’" Here is my code:

void main()
{
    char str1[100] = "ABC";
    char str2[100] = "XYZ";
    char str3[100] = "XAYZBC";

    printf(interleaved(str1, str2, str3));
}
bool interleaved(char str1[], char str2[], char str3[])
{
    int i = 0, j = 0, k = 0;

    if (strlen(str1) + strlen(str2) != strlen(str3))
        return false;

    while (k < strlen(str3))
    {
        if (i < strlen(str1) && str1[i] == str3[k])
            i++;

        else if (j < strlen(str2) && str2[j] == str3[k])
            j++;
        else
            return false;

        k++;
    }

    if (!(i == strlen(str1) && j == strlen(str2) && k == strlen(str3)))
        return false;

    return true;
}

I found the faulty part and tried to fix it as follows:

void main()
{
    char str1[100] = "ABC";
    char str2[100] = "XYZ";
    char str3[100] = "XAYZBC";

    bool result = interleaved(str1[100], str2[100], str3[100]);
    printf(result);
}

Can anyone help me please ?

Quentin
  • 62,093
  • 7
  • 131
  • 191
root_roxox
  • 179
  • 1
  • 9
  • Is the function `interleaved` is write before or after the main scope? – DipStax Oct 05 '20 at 14:54
  • Put the `bool interleaved(char str1[], char str2[], char str3[])` function _before_ `main`, or declare it like this `bool interleaved(char str1[], char str2[], char str3[]);` before `main`. **Always** consider this warning as an error. – Jabberwocky Oct 05 '20 at 14:54
  • @DipStax after the main scope – root_roxox Oct 05 '20 at 14:55

3 Answers3

2

"Implicit declaration" usually means, at the time when you are trying to use (call) a function, the compiler does not know the function signature / prototype, hence cannot determine the return type and cannot do any check on the argument passing.

Most of the time this is solved by either

That said, your function call and usage of printf() is not proper. You can do something like

bool result = interleaved(str1[100], str2[100], str3[100]);
printf("%s", result?"yes":"no");

Which prints "yes" is the function returns true, and "no" otherwise.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

You have to declare or define functions to use before using them.

Also bool is not a correct data to pass as the first argument of printf().

Try this:

bool interleaved(char str1[], char str2[], char str3[]); /* declare the function */

int main(void) /* use standard type for main() */
{
    char str1[100] = "ABC";
    char str2[100] = "XYZ";
    char str3[100] = "XAYZBC";

    printf("%s\n", interleaved(str1, str2, str3) ? "true" : "false"); /* correctly use printf() */
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

You are trying to use the result of interleaved (which is a bool) as the format string for printf, which is not allowed.

Instead, you need to do something like this:

printf("%d", interleaved(str1, str2, str3));

Also, you need to at least declare interleaved before you make a call to that function.

cigien
  • 57,834
  • 11
  • 73
  • 112