Solved

Need help with strcmp.

Posted on 1999-01-05
7
296 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 30 total points
ID: 1255564
Check the pointer before strcmp(a,b)

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

778 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