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

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";
}

Related

How can we declare short int as the return type in main function of C++? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Return type of main function [duplicate]
(5 answers)
Closed 11 months ago.
I am always returning a value of 0 from the main function. I was wondering if we could change the return type of the C++ main method to short int.
#include <iostream>
using namespace std;
short int main() {
cout << "Hello World" << endl;
return 0;
}
Something like this, but the above code returns an error saying that the return type should be 'int'. Can someone please guide me?

Garbage Value of a defined namespace [duplicate]

This question already has answers here:
How to print function pointers with cout?
(7 answers)
Why am I always getting output as 1 when printing a function?
(2 answers)
Closed 12 months ago.
So, I just started with C++ and was doing random things with a snippet of code I found on the net. The code is a simple use of defining namespace. After my changes in the code, it looked like,
#include <iostream>
using namespace std;
namespace ns1 { int value() {return 5;}}
namespace ns2 { int value() {return -5;}}
int main() {
cout << ns1::value<< '\n'; //5 will be displayed
cout << ns2::value<< '\n'; // -5 will be displayed
}
Now, i know that i have called the wrong function and it should be ns1::value() instead of ns1::value , but my question is why is the code still working? And why is it giving an output of 1?

Why do defining type name is optional in an if statement? [duplicate]

This question already has answers here:
Equality-test std::string against char*, is operator==() always safe?
(3 answers)
Closed 2 years ago.
Saw several examples that doesn't define type name for Y side and I used to to think C++ is type-strict. Is it safe to use a code snippet like below one in production?
I also curious to hear why it works..
#include <string>
int main() {
std::string X = "car";
if (X == "nothing") {
return 1
}
else if (X == "car") {
return 0
}
}
Thanks to operator== overload, std::string can compare to char* as well as to other std::string.
This is perfectly OK.

Why two different outputs? [duplicate]

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 5 years ago.
I am novice in programming. I have written a program and confused in concepts of pointers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c[]="hello";
char *a=c;
cout<<a<<endl;
int arr[]={1,2,3,5};
int *p=arr;
cout<<p<<endl;
return 0;
}
When I print a, it prints hello but when I print p it print the address. Why?
std::ostream has overload for const char* to display C-string.
int* would use the void* one which print the address.

Memory address (reference) not being displayed in terminal [duplicate]

This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 8 years ago.
I am trying to output a simple char address on the display, but using & is not working:
#include <iostream>
using namespace std;
typedef int no;
typedef char nose;
typedef void laps;
laps noname(nose &boogers);
no main(no args, nose**LOC[])
{
cout << "Try ";
nose boogers = 't';
noname(boogers);
}
laps noname(nose &boogers)
{
cout << &boogers;
}
I have tried it by removing the ampersand of the parameter of laps, but the &boogers datum is not working either way because on the console it just shows the "t" character instead of an address.
Am I doing something wrong here?
JSYK: It compiles fine, no warnings at all. I just want to know why I am not getting an address instead of a value.
After a reference is defined, it is not possible to refer to the reference itself, only the object it references. This is just how c++ operates. I suspect this is because references are not meant to be changed, this is one of their aspects which distinguishes them from pointers. Changing the memory address is prevented by not allowing you to access it.