1
votes

When I compile this code:

void rep_and_print(char * str, char * patt, int l, int i)
{
    char * pch; // pointer to occurence
    char * s;
    s = str; // save original pointer
    if (i == 0)
    {
        while ( (pch = strstr(str,patt)) != NULL)
        {
            // FOUND
            memset (pch,'*',l); // Set asterisk
            str = pch + l; // move pointer to the end of asterisks to found new  occurences
        }
    }
    else
    {
        while ( (pch = strcasestr(str,patt)) != NULL)
        {
            // FOUND
            memset (pch,'*',l); // Set asterisk
            str = pch + l; // move pointer to the end of asterisks to found new occurences
        }
    }
    printf ("%s",s);
}

I got this error:

warning: assignment makes pointer from integer without a cast [enabled by default]

while ( (pch = strcasestr(str,patt)) != NULL)

and there is an arrow point to the equal sign that is between pch and strcasestr

2
Do you also get warnings when you compile with -Wall? strcasestr isn't a standard function and may not be defined in <string.h>, so that your compiler assumes that it returns an int. That would explain why ther isn't an error for the same code with strstr. - M Oehm

2 Answers

2
votes

From the man page:

   #define _GNU_SOURCE

   #include <string.h>

   char *strcasestr(const char *haystack, const char *needle);

You need to add #define _GNU_SOURCE before you #include <string.h> (and #include <stdio.h> as well) in order for the function declaration to be visible.

0
votes

while ( (pch = strcasestr(str,patt)) != NULL) strcasestr is a nonstandard extension, so you must add #define _GNU_SOURCE on the first line of your file, or compile with -D_GNU_SOURCE.