Why do i need to add an asterisk? [duplicate] - c++

This question already has answers here:
Why can a C++ iterator be dereferenced although it isn't a pointer?
(3 answers)
Closed 3 years ago.
I have taken this exaple from cplusplus.com. I have this code :
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
std::cout << *it;
std::cout << '\n';
return 0;
}
What purpose does the asterisk after "cout<<" have? If i remove that asterisk it gives me error C2679 in VC++, which commonly apears when you forget to include <string>, but in my case i have included <string>.

The asterisk in this case means "dereferencing". That is; don't print the iterator, print what the iterator points to (just like you'd dereference a pointer to print what it points to rather than just printing the value of the pointer).

Related

How does the compiler determine what value to output during runtime when a function name is sent to cout without parenthesis? C++ [duplicate]

This question already has answers here:
g++ "calling" a function without parenthesis (not f() but f; ). Why does it always return 1?
(2 answers)
Closed 2 years ago.
#include <iostream>
int returnFive()
{
return 5;
}
int main()
{
std::cout << returnFive << '\n';
return 0;
}
Since this compiles without error, how does the system determine what value is actually sent and printed to console?
Imagine if the code written is something like
if(returnFive)
returnFive();
Here the expectation is that the compiler checks if the function pointer for returnFive is nullptr or not.
The compiler here is evaluating the function pointer as a boolean expression of whether it is NULL or not and printing the output.
https://godbolt.org/z/Psdc69. You can check that the cout is being passed a (bool).

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.

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

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.

Confusion about Dereferencing and Incrementing [duplicate]

This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 7 years ago.
just today I stumbled across this short piece of code that confused me a little.
#include <iostream>
#include <iterator>
int main()
{
int array[] = {0,1,2,3,4,5,6,7,8,9};
auto start = std::begin(array);
while (start != std::end(array))
std::cout << *start++ << std::endl;
}
The thing that is confusing me here is that the 0 is the first output. I read a lot of posts regarding the order of the 2 operators and every single one said: "start" would be incremented first, THEN dereferenced. But std::begin() returns an iterator to the beginning of the array. With this being said, if I increment the pointer to the beginning of the array first before dereferencing it, shouldn't my first output be the 1?
Thanks in advance!
*start++ uses the post increment operator. With post increment the item is incremented but the value returned is the value before incrementing. You are dereferencing that value so that is why the output starts at 0.

Changing contents of a std::string with a function [duplicate]

This question already has answers here:
When to use references vs. pointers
(17 answers)
Closed 9 years ago.
I feel like this answer will be quick and simple, but I cannot seem to figure it out right now.
#include <string>
#include <iostream>
using namespace std;
void change_thing (string x) {
x="not thing";
}
int main() {
string maybe_thing;
maybe_thing="thing";
change_thing(maybe_thing);
cout << maybe_thing << endl;
return 0;
}
I want maybe_thing to be "not thing" when it prints. I've tried a bunch of different pointer strategies, but nothing seems to work (which I could easily just be doing wrong; my knowledge of pointers is incomplete anyway because I'm new to c++).
Thanks in advance!
You don't need a pointer, just pass the string by reference:
void change_thing (string & x) {
x="not thing";
}