0

I have a problem with warning c4133, it say that the problem is incopatible types from char* to int*, I try to cast the pointer ((char*)x) but with no luck, maybe someone knows whats the problem/

this is my program, the problem in the function.

void replaceSubstring(char *str, char *substr)//function that gets string and substring and      make the substring in string big letters
{
    int i;
    int  *x;

    x = (strstr(str, substr));//The problem line 
    while (strstr(str,substr) != NULL)
    {
        for (i=0;i<strlen(substr);i++)
        {
            *x = *x - 32;
            x++;//move to the next char
        }
        x = (strstr(str, substr));  //first apear of substr int str
    }
 }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Tania Galan
  • 3
  • 3
  • 6

2 Answers2

7

in your code, x is defined as int * but the return type of strstr() is char *.. Check the man page here.

Worthy to mention, casting is usually considered a bad practice in c. With properly written code, most of the cases, casting can be avoided. Most of the cases, casting may introduce lot of bugs. Double check the data types and stay away from casting.

Sidenote: Just a suggestion, maybe before directly subtracting 32 from *x, maybe you want to perform a range check on *x to be within 97-122 to ensure that is a lower-case letter.

Also, better to perform strlen(substr) outside the loop, store into a variable and use that value in looping. Will save you the overhead of redundant calls to strlen().

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

Prototype of strstr is

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

Return value is character pointer. So make the x as a character pointer.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31