DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: OutPut of a Program
*****************************************************
#Post#: 180--------------------------------------------------
Predict the Output:
By: kranthipls Date: November 20, 2012, 11:19 pm
---------------------------------------------------------
#include<stdio.h>
void main()
{
char ch1[]="hello", ch2[]="hello";
if (ch1==ch2)
printf("Equal\n");
else
printf("Unequal\n");
}
What happens if the code is run using C++???????????
#Post#: 181--------------------------------------------------
Re: Predict the Output:
By: ravu Date: November 21, 2012, 12:27 am
---------------------------------------------------------
Unequal
#Post#: 184--------------------------------------------------
Re: Predict the Output:
By: kpr29 Date: November 21, 2012, 12:45 am
---------------------------------------------------------
Unequal......... Because we are comparing 2 different pointers
rather address not the value........
#Post#: 185--------------------------------------------------
Re: Predict the Output:
By: satya Date: November 21, 2012, 12:45 am
---------------------------------------------------------
y?? ??? ??? whats the reason?? :-[
#Post#: 187--------------------------------------------------
Re: Predict the Output:
By: dinesh Date: November 21, 2012, 8:49 am
---------------------------------------------------------
sometimes if the same string constant is stored in 2 or more
variables, they point to a single location. I think this happens
in Java.
#Post#: 190--------------------------------------------------
Re: Predict the Output:
By: kranthipls Date: November 24, 2012, 10:13 am
---------------------------------------------------------
Ok Agreed that it is not equal.
Now let us modify the question slightly.
Suppose if we change this into a c++ programs (making it a cpp
file, adding the necessary headers and making the return type
as int )
What happens if I change the char to string ?
#Post#: 202--------------------------------------------------
Re: Predict the Output:
By: kpr29 Date: November 26, 2012, 4:27 am
---------------------------------------------------------
@Kranthi: It becomes equal...............
#Post#: 210--------------------------------------------------
Re: Predict the Output:
By: kranthipls Date: November 26, 2012, 10:23 pm
---------------------------------------------------------
Just change it and see you'll get unequal, I mean you just need
to change the char word to string (within main function) nothing
else........ and include c++ headers on the top...
#Post#: 214--------------------------------------------------
Re: Predict the Output:
By: kpr29 Date: November 27, 2012, 12:14 am
---------------------------------------------------------
@Kranthi: For string ch1 = "hello", ch2 = "hello"........... The
answer is Equal.
For string ch1[] = "hello", ch2[] = "hello"............
The answer is unequal......... Is it pointer or
something.......... needs explanation.
#Post#: 216--------------------------------------------------
Re: Predict the Output:
By: kranthipls Date: November 27, 2012, 3:34 am
---------------------------------------------------------
When we take it as string ch1[], ch1 is not a single object
of class string but it is an array of objects of type string...
When we just give string ch1, ch1 is just an object of the
string class.
So in the above case ch1 will again be a
pointer...................
*****************************************************
DIR Next Page