C or C++. How to compare two strings given char * pointers?

前端 未结 8 1408
南笙
南笙 2020-12-07 01:01

I am sorting my array of car two ways. one by year which is shown below. and another one by make. Make is a char* How do I compare strings when I just have pointers to the

8条回答
  •  广开言路
    2020-12-07 01:58

    I'm of course assuming here you have char * for the car makes

    int i, j;
    for(i=0; i<100; i++){
        for(j=0; j<100-i; j++){
            if(carArray[i]!=NULL && carArray[j]!= NULL && carArray[j+1]!=NULL){
                if(strcmp(carArray[i]->make, carArray[j+1]->make) == 0)
                {
                    //Do whatever here
                }
            }
        }
    }
    

    You want to compare against 0 because strcmp will return 0 if there is no difference between the two strings.
    strcmp takes two const char *.
    http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

提交回复
热议问题