Thread: makro to compare strings

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    makro to compare strings

    I'm trying to make a makro to compare strings.
    And yeah, I admit it, it's a school-task.
    But it's just a task we were given "for fun" by a fellow student that runs our group-exercises, it's nothing I have to do, or anything that will benifit me.


    We was asked to write a makro on this form:
    Code:
    #define STREQ(s1,s2) ((...))
    That can compare 2 null-terminated strings to check if they are equal.
    It should also be able to be used inside if's like this:
    Code:
    if(STREQ(string1, "TEST")) ...
    I have thought of this for several hours now, and still havent got anywhere.
    I first thought of this solution:
    Code:
    #define STREQ(s1,s2) ((s1 == s2) 1 ? 0)
    But this only checks the adress of the memory-adress of the first element in each array.

    I then thought of doing something ugly like this:
    Code:
    #define STREQ(s1,s2) \
    { \
    for(int i=0; i < strlen(s1); i++) \
    } \
    It's not complete, but you get the idea... but I couldent get this to work, because it have to be able to expand inside an IF.

    The only thing I can think of now, is to wrap a searching-function inside the STREQ makro...but that would remove the whole point of making a makro like this.
    And when Im into that topic, I really dont see the point of learning stuff like this at the university, but oh well.

    In any case, I am very interested in knowing how to do this ........, after trying for hours.
    Or even better, if anyone could confirm my theory that this task is impossible

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you allowed to use strcmp() in your macro?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Are you allowed to use strcmp() in your macro?
    There's no rule against it, but what's the point of using strcmp() in a macro like this?

    Code:
    if(strcmp(stringA,stringB) == 0)) ...
    
    // versus
    
    if(EQUALMAKRO(stringA,stringB)) ...
    I would preffer just to use the strcmp without a macro-wrapping... it's a standard function everyone knows, and the efficiency of the program at runtime would be the same.

    But maybe I am thinking more than they intend for me to do here, and that the solution is as simple as wrapping in a compare-function
    Last edited by Drogin; 09-02-2009 at 04:27 PM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Okay, I'm not going to give you the answer, but look at the following example (C++, but should be obvious: )
    Code:
    #include <iostream>
    using namespace std;
    
    #define TEST(x) ((x++, 123))
    
    int main()
    {
      int b = 2;
      int a = TEST(b);
      cout << a << " " << b << endl;
    }
    This works on my compiler at least (g++), I don't know if it always works. The trick is that whatever is after the comma is treated as the "return value" of the macro.


    And while I don't particularly like "if(!strcmp(...))" I don't dislike it either. I do dislike creating a string-equal function or macro. The best solution is just to use "if(strcmp(...) == 0)". That's readable just fine, for anyone who is not too much of a newbie. The faq is wrong as to say the first one looks like "not equal". Anybody who knows C a bit knows it means "not different". Though I read not different as "not not the same", and the double not I try to avoid when coding. Hence I wouldn't use the first one myself.
    Last edited by EVOEx; 09-02-2009 at 05:38 PM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by EVOEx View Post
    This works on my compiler at least (g++), I don't know if it always works. The trick is that whatever is after the comma is treated as the "return value" of the macro.
    it is not a trick, it just using " operator ," of C.

    You can read the description of this operator in C reference guide. It could be freely used outside macro definitions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by vart View Post
    it is not a trick, it just using " operator ," of C.

    You can read the description of this operator in C reference guide. It could be freely used outside macro definitions
    I know about the operator ,. That's how I found out about this. I called this a trick because it's the "trick" to solve this program. Of course it's just a logical expansion that results in this, but it's still something you have to know in order to solve this problem.
    And while this is standard compliant, the macro will have to have the comma operator inside a code block, in between the curly braces. That works as well, but I'm not 100% sure that's standard as well. I expect it is, but I'd rather inform the reader if I'm not completely sure about it.

    I mean something like this:
    Code:
    int a = ({ 123, 456; });

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. how do i compare first letters in two strings?
    By MalickT in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 05:47 PM
  3. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 12:51 PM
  4. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 12:27 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM