views:

420

answers:

7

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 char pointers?

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(carArray[i]->year > carArray[j+1]->year){
                swap(carArray[j], carArray[j+1]);
            }
        }
    }
}

The above method works for int's (year). How can I make it work for char pointers?

+12  A: 

I think you need to use the strcmp() function.

sklitzz
+16  A: 

In pretty much either one, the way is to call strcmp. If your strings (for some weird reason) aren't nul terminated, you should use strncmp instead.

However, in C++ you really shouldn't be manipulating strings in char arrays if you can reasonably avoid it. Use std::string instead.

T.E.D.
Since you're sorting, std::sort is your friend too. http://www.cplusplus.com/reference/algorithm/sort/ All you need to supply is a comparison function for std::sort to use.
carleeto
+2  A: 

Make sure the char * isn't null, and if you want, look for the stricmp() function for case insensitive comparisons. Otherwise, use strcmp().

char * actually represents the memory address of the first character in each string. So you don't really want to be comparing the values of the pointers, but the contents they point to.

Walt Stoneburner
stricmp() isn't standard: http://stackoverflow.com/questions/1784767/g-error-stricmp-was-not-declared-in-this-scope-but-ok-for-strcmp I think it's Microsoft-specific.
Fred Larson
If stricmp() isn't in your library, also check for strcmpi() and if that's not there strcasecmp(). Something with that functionality will be there.
Walt Stoneburner
+1  A: 

In C its strcmp() function as already stated. In C++ you can use the compare() function.

C:

 char str1[10] = "one";
 char str2[10] = "two";

 if (strcmp(s, t) != 0) // if they are equal compare return 0

C++

 string str1 ("one");
 string str2 ("two");
 if (str1.compare(str2) != 0) // if they are equal compare return 0
Milan
Uhhh, no you shouldn't use `compare` here. This is what the `==` operator on strings is for. The only reason to use compare is when you care about whether one string is (alphabetically) less than or greater than the other.
T.E.D.
A: 

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/

Craig
A: 

You should really use qsort (in C, #include ) or std::sort (in C++, #include ) instead of a bubble sort like this. If it's C++ and you take @T.E.D.'s advice to use std::strings instead of raw C strings, you don't even have to specify a comparison because the < operator will be used and will do the right thing.

Scott Wolchok
A: 

When you need to compare two char pointers specifically, you can compare them in the usual way: by using comparison operators <, >, == etc.

The issue in ths case is that you don't need to compare two char pointers. What you do need though, is to compare two C-style strings these char pointers are pointing to. In order to compare the C-style strings, you have to use the standard strcmp function.

On top of that, the approach to handling null elements in your sorting algorithm doesn't seem to make any sense whatsoever. Imagine an input array that contains alternating null pointers and non-null pointers. It is obvious, that your sorting algorithm will never sort anything, since the condition in your if will never be true. You need to reconsider your handling of null elements. Of course, first of all, you have to decide what to do with them. Ignore and leave in place? Push to one end of the array? Somethng else?

AndreyT