Thread: string mismatch issue when using if condition

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    23

    string mismatch issue when using if condition

    Hello everyone.

    I have been trying to solve one competitive problem on Contest Page | CodeChef.
    While trying to compare two strings, I used if statement
    Code:
    if(ar[i].string==ar[j].string)
    instead of strcmp
    Code:
    if(strcmp((ar[i].string), ar[j].string) == 0)
    .

    But the result seems to be different even when both the strings are same. Once I change the if statement with the strcmp, it works perfectly and as per expectation.

    Here is the snippet of code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef unsigned char u8;
    typedef unsigned int u16;
    typedef unsigned long u32;
    typedef char s8;
    typedef int s16;
    typedef long s32;
    
    #define scan(x) int x; scanf("%d", &x);
    #define lscan(x) u32 x; scanf("%ld", &x);
    #define scanloop(ctr,x)  {int i; for(i=0;i<ctr;i++) scanf("%d", x);}
    #define printloop(ctr,x) {int i; for(i=0;i<ctr;i++) printf("%d\n", x);}
    #define printdata(x) printf("%d\n", x);
    #define selfprint(x) printf(x);
    
    
    #if 1
    
    
    int main()
    {
        typedef struct sr
        {
            u8 string[20];
        }sr;
    
    
        lscan(loop)
        while(loop--)
        {
            lscan(n)
            sr ar[100001]={0};
            u32 ctra=0, ctrb=0,indexa=0, indexb=0;
    
    
            for(int i=0,j=0; i<n; i++)
            {
                scanf("%s", ar[i].string);
            }
            for(int i=0,j=0; i<n; i++)
            {
              if(ar[i].string==ar[j].string)  //not working statement
              //if(strcmp((ar[i].string), ar[j].string) == 0) //Issue seems to be here
              {
                ctra++;
                indexa = i;
              }
              else
              {
                ctrb++;
                indexb = i;
              }
            }
    
    
            if(ctra>ctrb)
            {
                printf("%s\n", ar[indexa].string);
            }
            else if(ctrb>ctra)
            {
                printf("%s\n", ar[indexb].string);
            }
            else
                selfprint("Draw\n")
        }
        return 0;
    }
    #endif // 1
    Could anyone tell why it is so? Answers seems different when I entered:

    Code:
    2
    4
    ab
    bc
    bc
    ab
    When I enable the if condition, result become "ab" but when I used strcmp if becomes "Draw" which is expected.

    Apology if question seems stupid also if code looks chaos but please let me know if something bothered anyone.

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,558
    If you use == you end up comparing pointers rather than the strings themselves.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,755
    > #define scan(x) int x; scanf("%d", &x);
    Don't do this.

    See Question 10.2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    23
    Thanks mate. That help's
    Quote Originally Posted by laserlight View Post
    If you use == you end up comparing pointers rather than the strings themselves.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libtool version mismatch
    By Mario F. in forum Tech Board
    Replies: 0
    Last Post: 10-08-2015, 11:20 AM
  2. Replies: 2
    Last Post: 08-06-2012, 08:59 AM
  3. Type mismatch warning
    By Opel_Corsa in forum C Programming
    Replies: 5
    Last Post: 10-11-2006, 10:34 AM
  4. Replies: 7
    Last Post: 07-18-2005, 08:43 AM
  5. best way to base condition on string
    By Kinasz in forum C Programming
    Replies: 6
    Last Post: 07-26-2004, 05:12 PM

Tags for this Thread