Solved

Need help with strcmp.

Posted on 1999-01-05
7
320 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

705 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