Link to home
Start Free TrialLog in
Avatar of gschetrit
gschetrit

asked on

Need help with strcmp.

I’m using an array of void* in my program. This array is filled with strings in several places (e.g. place 2,5,7). The size of the array is 10. What I have to do is to find the max and min values of that array. I’m using the lib function strcmp but have a problem accomplishing the goal. The problem that I have is that I check the value returned from the function and compare it to zero. In the blank places in the array there are NULL poiters and I don’t really know how to deal with what I get from the function when it compares a NULL with a string or a NULL with a NULL. if (0<strcmp(str1, str2)) return.. what should I do?
Is there a way to find the min and max of that array???
Avatar of plsbctv
plsbctv

Check the pointer for null before you use strcmp. If the pointer is null, then that value is neither smallest or largest, so just skip it.

Tell me this isn't a homework problem and I'll write the code.

    ++PLS
Avatar of gschetrit

ASKER

that's the code I'm using. str1 and str2 might be NULL pointers.
it works for no NULLs.
#include <stdio.h>

#include "string~1.h"

int string_compare(void* str1, void* str2) {

                     if (str1==NULL) return 1;

            return (strcmp((char*)str1, (char*)str2));

}


that's the code I'm using. str1 and str2 might be NULL pointers.
it works for no NULLs.
#include <stdio.h>

#include "string~1.h"

int string_compare(void* str1, void* str2) {

                     if (str1==NULL) return 1;

            return (strcmp((char*)str1, (char*)str2));

}


that's the code I'm using. str1 and str2 might be NULL pointers.
it works for no NULLs.
#include <stdio.h>

#include "string~1.h"

int string_compare(void* str1, void* str2) {

                     if (str1==NULL) return 1;

            return (strcmp((char*)str1, (char*)str2));

}


its working. thanks for the help anyway....

What will be the return value if
str1 contains "BBB"
and
str2 contains "AAA".
Will it still work?
ASKER CERTIFIED SOLUTION
Avatar of tato
tato

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial