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.
Related
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?
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Closed 6 years ago.
If I understand correctly, variables that are not dynamically allocated are supposed to be deleted at the end of their scope.
However, I wanted to try it out with this code (which I think is not correct as I am supposed to use dynamic allocation) :
int* function()
{
int a = 3;
return &a;
}
int main(int argc, char const *argv[]) {
int* a(function());
std::cout << *a << std::endl; // prints 3
}
Why can I still access the value of the variable a by using the pointer returned by the function when it is supposed not to exist anymore ?
a and hence the return value from function has gone out of scope. You are just lucky.
Just be careful and compile with all the warnings enables - and take heed of those warnings.
The fact that you can access the value is pure luck. The code has undefined behaviour (since the variable is destroyed) and the compiler can generate whatever it wants - but you cannot rely on it.
This question already has answers here:
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Closed 7 years ago.
As it says here: How to use delete with a variable pointed to by two pointers?
one can only delete once, then all pointers won't work, but the code below:
#include<iostream>
using namespace std;
int main(){
string * str1 = new string("abc");
string * str2 = str1;
cout<< *str2 <<endl;
delete str1;
str1 = NULL;
cout<< *str2<<endl;
}
the output is:
abc
abc
so, what's the matter?
Once you have deleted a pointer, accessing it is undefined behavior and may print old contents sometimes, may crash sometimes or may do something beyond thinking some other time.
To handle a case of deleting shared pointer, use std::shared_ptr or similar reference counting manager wrapper instead of naked pointers.
One of the outcomes of undefined behavior is that is might seem to work. And dereferencing a pointer to a destructed object (like you do with cout<< *str2<<endl;) is undefined behavior.
This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main()
{
const int kiNum = 100;
int* ptr = const_cast<int*>(&kiNum);
*ptr = 200;
cout<<"kiNum: "<<kiNum; // The value still prints 100 on the console??
return 0;
}
output:
kiNum = 100
In the above code snippet , i am trying to change the value of a const integer, after const_cast and then change the value at the address, but the console still prints the old value (i am using visual studio 2012)
Writing to something which is defined as const is undefined (assuming you cast away the const of course).
http://en.cppreference.com/w/cpp/language/const_cast
It's a pretty accurate website. If you have issues with a language feature its always worth looking up there IMHO.
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.