0

well I've got this code that I'm trying to get working but no matter what I keep on getting that I am getting 0xC0000005

int main()
{
    if(stringContains("hello", "Hello world", FLAG_CASE_SENSITIVE))
    {
        printf("Works");
    }
    
    printf("%i", stringContains("hello", "Hello world", FLAG_CASE_SENSITIVE));
    return 0;
}

#define FLAG_CASE_INSENSITIVE 0
#define FLAG_CASE_SENSITIVE 1
    
typedef enum { false, true } bool;
    
bool stringContains(char* needle, char* stack, int type);
char* toLower(char* s);
    
bool stringContains(char* needle, char* stack, int type)
{
    if(type == FLAG_CASE_SENSITIVE)
    {
        return (strstr(toLower(stack), toLower(needle)) != 0) ? true : false;
    }
    return (strstr(stack, needle) != 0) ? true : false;
}

char* toLower(char* s) {
    for(char *p=s; *p; p++) *p=tolower(*p);
    return s;
}

I must admit, I am pretty basic when it comes to C

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
Tomislav Tomi Nikolic
  • 608
  • 3
  • 10
  • 15
  • 5
    You are passing *string literals* to functions that attempt to modify them. String literals are read only. – Eugene Sh. Nov 24 '20 at 22:01
  • OT: You need to declare functions before they are called. That is, move the forward declarations to the top of the file. – kaylum Nov 24 '20 at 22:04
  • @kaylum I did, it's just I merged few files, thats why it looks like this. – Tomislav Tomi Nikolic Nov 24 '20 at 22:05
  • @EugeneSh. Hmm alright, what should I do? I'm really new to this :( – Tomislav Tomi Nikolic Nov 24 '20 at 22:05
  • Either don't pass string literals to those functions, or modify the functions so that they don't attempt to modify the strings. Personally, I prefer the latter approach. – Robert Harvey Nov 24 '20 at 22:06
  • First of all, there is no reason for the functions to modify the input. You are only *checking* something. This procedure should not be destructive. – Eugene Sh. Nov 24 '20 at 22:07
  • @TomislavTomiNikolic Next time please show the exact code that you have compiled and run that can reproduce the problem. It is very important as details matter in programming. As for the string literal problem, see: [Difference between char* and char array](https://stackoverflow.com/questions/7564033/difference-between-char-and-char) – kaylum Nov 24 '20 at 22:07

1 Answers1

1

Try to declare functions which will accept literals as const`:

bool stringContains(const char *needle, const char *stack, int type);

Then you will be warned if the const will be stripped when you call another functions.

https://godbolt.org/z/x4z5vE

You will need to get rid of this problem. Cast will not help as it will only silence the warning but not change anything regarding the strings, so you will need to create the writable copies of the strings.

bool stringContains(const char* needle, const char* stack, int type)
{
    
    if(type == FLAG_CASE_SENSITIVE)
    {
        char *haystack = strdup(stack);
        char *newneedle = strdup(needle);
        char *result = NULL;
        if(haystack && newneedle)
            result = strstr(toLower(haystack), toLower(newneedle));
        free(newneedle);
        free(haystack);
        return !!result;
    }
    return !!strstr(stack, needle);
}

https://godbolt.org/z/Kvdxq3

0___________
  • 60,014
  • 4
  • 34
  • 74