Solved

Need help with strcmp.

Posted on 1999-01-05
7
279 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use conditional statements in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now