Please explain how this c++ program is working? [duplicate] - c++

This question already has answers here:
"" + something in C++
(3 answers)
Closed 5 years ago.
I am having trouble , in understanding this program please help:
#include <iostream>
using namespace std;
int main(){
const char* s = 5+"hellow world";
cout<<s;
return 0;
}
It is correct and gives following output

In third line of your code, an anonymous character array is created by the compiler. When you add 5 to the c-string, it performs pointer arithmetic and moves the pointer 5 ahead to the string. Hence, it skips the 5 character from the c-string and only stores other characters from the array into the s.

Related

Using & with string class objects in c++ [duplicate]

This question already has answers here:
displaying address of char variable in c++ using pointers?
(1 answer)
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 9 months ago.
As far as my knowledge in c++, the & character can act as an address of operator(finding the address of a variable in memory) or as a bitwise AND operator or declaring references.
However if I run this code:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "Stackoverflow";
cout<<&s[0]<<endl<<&s[1]<<endl;
return 0;
}
Output is
Stackoverflow
tackoverflow
I expected it to print the addresses of the first 2 characters of the string, however, I got the string itself starting from a different index. How does it work?

Why sizeof() method is giving different results? [duplicate]

This question already has answers here:
Size of character ('a') in C/C++
(4 answers)
Closed 2 years ago.
When I run the program below in C, I get the output result to be 4.
#include <stdio.h>
//using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
But when I run the code below in C++, I get the output result to be 1.
#include <iostream>
using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
Could you please explain why do I get different output for the same code as if 'a' is the way we define characters in both the languages ?
In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.
In C++, it's of a character type.

Is my compiler incorrect? [duplicate]

This question already has answers here:
Using the equality operator == to compare two strings for equality in C [duplicate]
(9 answers)
Closed 4 years ago.
Whenever I run this in the compiler I get 1 returned to me. However, I've been told that this shouldn't return 1 and that my compiler is wrong and can't be trusted.
Is my friend right in that the compiler's I use are giving me the incorrect answer, or is this supposed to return 1?
#include <iostream>
#include <string>
int main()
{
bool lol = "abc" < "abcd";
std::cout << lol;
return 0;
}
The code has Undefined Behaviour, because it's using an oredering comparison operator on two pointers (the address of the string literal "abc" and that of the string literal "abcd") which are not part of the same array. It can therefore do absolutely anything.

program in c++. size of char array is 5 but value more than 5 char is also accepted [duplicate]

This question already has answers here:
Why does this work? Using cin to read to a char array smaller than given input
(2 answers)
Closed 7 years ago.
this is a short program in c++
#include<iostream>
using namespace std;
int main(){
char s[5];
cout<<"enter your name"<<endl;
cin>>s;
cout<<"Hello"<<s<<endl;
return 0;
}
value greater than 5 are also accepted
what is the concept behind it??
You're indeed correct. To attempt to put any more than 5 characters (including a null-termimator) into s is undefined behaviour.
In C++, the best thing to do is to use a std::string instead. Appropriate overloads for << and >> are provided for std::string.

Using & operator with char data type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is address of char data not displayed?
I was experimenting with ampersand operator and got stuck at this program :
#include<iostream>
using namespace std;
int main() {
char i='a';
cout<<&i;
return 1;
}
I was expecting the address of variable i as the output but instead the output came as the value of variable i itself.
Can anybody explain what just happened? Thanx in advance.
That's because cout::operator<< has an overload for const char*. You'll need an explicit cast to print the address:
cout<<static_cast<void*>(&i);
This will call the overload with void* as parameter, which is the one used to print addresses.
Also note that your code runs into undefined behavior. You only have a single char there, and the overload expects a null-terminated C-string.