0

Why this don't work? months[5] == name is equal. months[5] is Jun and name is Jun but if never execute...

 int getMonthNum(char * name){
    char *months[12] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    char *pointertoarray = &months;
    int i;
        for(i = 1; i <= 12; i++){
            if(months[5] == name){
                return i;
            }
        }
    return i;
    }
Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60

1 Answers1

4

use strcmp or else it will compare the pointer.

int getMonthNum(char * name){
    char *months[12] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
//char *pointertoarray = &months;
    int i;
        for(i = 0; i < 12; i++){
            if( strcmp(months[i], name)==0){
                return i;
            }   
        }   
    return i;
    }   

http://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm

resultsway
  • 12,299
  • 7
  • 36
  • 43