Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with strcmp.

Posted on 1999-01-05
7
Medium Priority
?
331 Views
Last Modified: 2010-04-15
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???
0
Comment
Question by:gschetrit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:plsbctv
ID: 1255558
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
0
 

Author Comment

by:gschetrit
ID: 1255559
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));

}


0
 

Author Comment

by:gschetrit
ID: 1255560
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));

}


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:gschetrit
ID: 1255561
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));

}


0
 

Author Comment

by:gschetrit
ID: 1255562
its working. thanks for the help anyway....

0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1255563
What will be the return value if
str1 contains "BBB"
and
str2 contains "AAA".
Will it still work?
0
 

Accepted Solution

by:
tato earned 60 total points
ID: 1255564
Check the pointer before strcmp(a,b)

if (ptrA && ptrB) x=strcmp(ptrA,ptrB);
else x=-1;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question