Answer to Question #9674 in C# for kim

Question #9674
Consider the code
char
*a = "xyx", *b = "xyz";
if ( a==b )
printf("The two strings have the same address!\n");
else
printf("As I expected, the addresses are different.\n");
The expression a==b compares pointer values, not the contents of the strings.
Also, the expression a=b is an assignment of a pointer value, not an assignment
of a string. In this exercise we want to consider = and == with respect to their
use with structures. Suppose now that a and b are two structure variables of the
same type. The expression a=b is valid, but the expression a==b is not. The
operands of the == operator cannot be structures. Write a small test program to
see what your compiler does if you try to use the expression a==b, where a and
b are structures of the same type.
1
Expert's answer
2012-11-12T07:02:57-0500
#include <iostream>
using namespace std;

struct testStructure {
};

int main ()
{
& testStructure test1;
& testStructure test2;
& cout << test1==test2;
& system("pause");
& return 0;
}
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'testStructure' (or there is no acceptable conversion)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS