Search code examples
cstrcmp

Strcmp crashes application


I have this function

int does_exist_in_array(char team[], struct team *teams) {
    int i;
    for(i = 0; i < MAX_TEAMS_AMOUNT; i++) {
        if(!strcmp(team, teams[i].name)) {
            return 1;
        }
    }
    return 0;
}

It crashes when i run the application. Anyone know what is wrong? Do i use it wrong?


Solution

  • This can happen for a number of reasons:

    • Either argument is NULL or otherwise an invalid pointer
    • The string pointed by either argument is not 0-terminated
    • There are fewer than MAX_TEAMS_AMOUNT team elements