0

what would be the output of the snippet code c++ and how would it be like that?

#include <iostream> using namespace std; int main() { typedef struct {char ch;} st; st s1={'c'}; st s2=s1; if (s1==s2) cout<<"sucessfull"; return 0; } https://code.sololearn.com/cuIdQ76ZDd6s

6th Jul 2022, 7:14 PM
Nariman Tajari
Nariman Tajari - avatar
3 Respuestas
+ 3
6th Jul 2022, 7:45 PM
Slick
Slick - avatar
+ 1
so you are asking what something like this: char s1{'c'}; char s2{s1}; if(s1==s2) cout << "successful"; would be? From your naming convention I assume you think these are some kind of "string", which they are not. The char is really almost exactly the same as an unsigned int8_t. Think about the logic with a regular integer, it would be the same value right? So the output would be "successful". Please notr that you can overload the == operator in C++. This is also often done in string classes. So even having s1 and s2 as std::string would result in the same out, even though they do have different mempry locations. That is a difference to languages like java, where you have to implemnet a method to compare two reference types. In C++ you can implement the operator for these types yourself directly. In java this operator would just compare the reference/pointer. In C++ you are always working with values though! Except you are using pointers. But be aware the string classes will have pointers as members
7th Jul 2022, 7:40 AM
Erarnitox
Erarnitox - avatar
+ 1
I have made your code runable and did some small changes: https://code.sololearn.com/cK2t4yn5ydFG/?ref=app
7th Jul 2022, 1:32 PM
Erarnitox
Erarnitox - avatar