GeeksforGeeks » Interview Questions

Adobe Interview Question for Software Engineer/Developer (Fresher) about Aptitiude, Arrays, Data Str

(6 posts)
  • Started 4 months ago by Shubhra
  • Latest reply from vaidy.mit
  1. Shubhra
    guest
    Posted 4 months ago #

    Predict the Output:

    #include<stdio.h>
    void main()
    {
            char ch1[]="hello", ch2[]="hello";
            if (ch1==ch2)
                    printf("Equal\n");
            else
                    printf("Unequal\n");
    }

    I executed this on Linux terminal and got the answer as Unequal. Can anyone please explain how this is happening?

  2. vasu
    guest
    Posted 4 months ago #

    coz array names are resoluted to their starting address. hence when you are comparing, you are actually comparing the addresses of two variables ch1 and ch2 which are different

  3. V.B.KARTHIKEYAN
    guest
    Posted 4 months ago #

    Hi,

    But in C there is a concept called "Shared Strings".

    That is, if the same string contains more then one pointers, and if Shared string feature is on,
    then both the ptrs refer to the same location in memory. In such cases, u ll get "equal".

  4. vengat
    guest
    Posted 4 months ago #

    Shared string works if the the code was as below ,
    #include<stdio.h>
    void main()
    {
    char *ch1 = "sample";
    char *ch2 = "sample";
    if (ch1==ch2)
    printf("Equal\n");
    else
    printf("Unequal\n");
    }

    I.e in case of pointer both point to same location but in case of arrays they point differ.

  5. PINTUGUPTAJUIT
    Member
    Posted 3 months ago #

    yes vangat you are right..

  6. vaidy.mit
    Member
    Posted 1 month ago #

    The actual answer for this is you cannot check two strings using == operator ,in case of strings you need to use string compare,that is the actual answer ..


Reply

You must log in to post.

RSS feed for this topic