Newbie
Newbie

Reputation: 1163

C++: How to make comparison function for char arrays?

Is this possible? i get weird error message when i put char as the type:

inline bool operator==(const char *str1, const char *str2){
    // ...
}

Error message: error C2803: 'operator ==' must have at least one formal parameter of class type ... which i dont understand at all.

I was thinking if i could directly compare stuff like:

const char *str1 = "something";
const char *str2 = "something else";
const char str3[] = "lol"; // not sure if this is same as above

and then compare:

if(str1 == str2){
   // ...
}

etc.

But i also want it to work with:

char *str = new char[100];

and:

char *str = (char *)malloc(100);

I am assuming every char array i use this way would end in NULL character, so the checking should be possible, but i understand it can be unsafe etc. I just want to know if this is possible to do, and how.

Upvotes: 2

Views: 4265

Answers (6)

Software_Designer
Software_Designer

Reputation: 8587

Use strcmp() to compare chars: It works on char pointers char * and char arrays char [ ] .

#include <string.h>
#include <stdio.h>

char *string_Char_pointer1 = {"2012-12-06 14:28:51"};
char *string_Char_pointer2 = {"1911-12-06 14:28:51"};

char string_Char_Array1[] = "2012-12-06 14:28:51";
char string_Char_Array2[] = "1911-12-06 14:28:51";

int main( void )
{
   char tmp[20];
   int result;

   printf( "Comparing  string_Char_pointer..\n\n\n");

   printf( "Compared strings:\n   %s\n   %s\n\n\n", string_Char_pointer1, string_Char_pointer2 );
   result = strcmp( string_Char_pointer1, string_Char_pointer2 );

   if( result > 0 )        strcpy( tmp, "greater than" );
   else if( result < 0 )   strcpy( tmp, "less than" );
   else    strcpy( tmp, "equal to" );

   printf( "   strcmp:   String 1 is %s string 2\n\n", tmp );

   printf( "\n\nComparing string_Char_Array..\n\n");

   printf( "Compared strings:\n   %s\n   %s\n\n\n", string_Char_Array1, string_Char_Array2 );
   result = strcmp( string_Char_pointer1, string_Char_pointer2 );

   if( result > 0 )        strcpy( tmp, "greater than" );
   else if( result < 0 )   strcpy( tmp, "less than" );
   else    strcpy( tmp, "equal to" );




   return 0;
}

Upvotes: 0

Edward Strange
Edward Strange

Reputation: 40877

You most certainly, totally can write your own comparison function, and in fact it's one of the most basic things to do. I have no idea why someone would say that you can't. You just can't call it operator =. Here it is (untested):

int my_strcmp(char const* s1, char const* s2)
{
  for(;*s1 == *s2 && *s1; ++s1,++s2);

  return *s1 - *s2;
}

...
if (!my_strcmp(str1, str2)) // they're ==.

Upvotes: 0

pstrjds
pstrjds

Reputation: 17438

Decided that my comment would be better as an answer, you should use the standard string functions for this (strncmp, strncat, etc).

Edit: As pointed out in another answer, you can't do the overload. But in the case of the char arrays and char pointers you should use the standard library functions.

Upvotes: -1

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84189

Pointers are built-in types. There are built-in comparison operators for them already, you cannot override them. Just use std::string.

Upvotes: 3

ypnos
ypnos

Reputation: 52367

It is not possible. As your compiler points out, you cannot overload this operator for primitive data types. At least one side of the comparison must be non-primitive for the overload to be possible.

In the same sense, you cannot derive a new class from a primitive data type (to add functionality to it).

Upvotes: 7

Moo-Juice
Moo-Juice

Reputation: 38810

You are attempting to compare two pointers.

const char* str1 = "string1";
const char* str2 = "string1";

if(str1 == str2) // never true, str1 is not the same pointer as str2
{
};

But, you've provided the C++ tag, so you should be using std::string:

#include <string>

std::string str1 = "string1";
std::string str2 = "string1";

if(str1 == str2)  // yes!  std::string overloads operator ==
{
}

Upvotes: 4

Related Questions