This question already has answers here:
unable to print char* pointing to 0
(4 answers)
Closed 9 years ago.
In Visual studio 2012, I was messing around with pointers, and I realized that this program kept crashing:
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
const char* pointer = nullptr;
cout << "This is the value of pointer " << pointer << "." << endl;
return 0;
}
My intent was the set a pointer to nullptr, and then print the address. Even though the program compiles, it crashes during runtime. Can someone explain what is going on?
Also, what's the difference between pointer and *pointer?
You are using a const char* which, when used in std::cout's operator <<, is interpreted as a string.
Cast it to void* to see the pointer's value (the address it contains):
cout << "This the value of pointer " << (void*)pointer << "." << endl;
Or if you want to be pedantic:
cout << "This the value of pointer " << static_cast<void*>(pointer) << "." << endl;
LIVE EXAMPLE
Although you can do cout << "Pointer is " << (void*)pointer << ".\n"; as already been suggested I feel that in this case the "C way" of doing it is prettier (no casting) and more readable: printf("Pointer is %p\n",pointer);
Related
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 2 years ago.
I am a newbie in c++ and my question may seem basic, but your answer could help me and help others.
I created to char pointer myPointer1 und myPointer2 so
const char *myPointer1 = "Hallo";
const char* myPointer2 = myPointer;
I thought that pointer stored the address of the variables they point to. In this case we have just one variable "Hallo" and both pointers should then points to the same address.
but when i print :
cout << &myPointer1 << endl << endl;
cout << &myPointer2 << endl << endl;
the results are two different adresses:
009EFC00
009EFBE8
Could anyone help?
You are printing the address of the pointer, not the address that the pointer points to.
std::cout << myPointer << std::endl;
This would print the address the pointer points to.
Since a char* is treated as a string when passed to std::cout it will print Hallo.
If you want to print the address itself you can achieve that by casting it to a const void* and printing that.
#include <iostream>
int main() {
const char *myPointer1 = "Hallo";
const char* myPointer2 = myPointer1;
std::cout << static_cast<const void*>(myPointer1) << std::endl;
std::cout << static_cast<const void*>(myPointer2) << std::endl;
}
My code:
#include <iostream>
using namespace std;
int main() {
char *test = (char*)malloc(sizeof(char)*9);
test = "testArry";
cout << &test << " | " << test << endl;
test++;
cout << &test << " | " << test << endl;
return 1;
}
Result:
004FF804 | testArry
004FF804 | estArry
I don't understand how it's possible that I had moved my array pointer and address didn't change.
The pointer did change. You're just not printing it. To print the pointer test:
cout << (void*) test << endl;
&test is the memory location where test is stored.
test is the value that you incremented with test++ (i.e., you didn't increment &test).
When you do cout << test the overload of operator<< that gets picked is one that takes a const char* and treats it as a C-style string, printing the characters it points to. The cast to void* avoids this behavior in order to print the actual value of test, instead of the value it points to.
In this statement
cout << &test << " | " << test << endl;
expression &test yields the address of the variable test itself that is evidently is not changed. It is the value stored in the variable was changed.
If you want to output the value of the variable test that is the value that points inside the string literal you should write
cout << ( void * )test << " | " << test << endl;
Take into account that there is a memory leak in the program because after allocating memory the pointer is reassigned. And sting literals have types of constant character arrays. So the pointer test should be declared like
const char *test;
I'm having some trouble understanding pointers. In the following code, I'm trying print the address of a variable in 2 ways-once using the address operator and then using pointers:
#include<iostream>
using namespace std;
int main (void)
{
int x = 10;
int *int_pointer;
int_pointer = &x;
cout << "x address=" << &x << endl;
cout << "x address w pointer=" << int_pointer << endl;
return 0;
}
x address = 0028FCC4
x address w pointer = 0028FCC4
This works as expected. But when I do the same thing but now using character type variable, I get some trash output:
#include<iostream>
using namespace std;
int main(void)
{
char c = 'Q';
char *char_pointer;
char_pointer = &c;
cout << "address using address operator=" << &c << endl;
cout << "address pointed by pointer=" << char_pointer << endl;
return 0;
}
address using address operator=Q╠╠╠╠£åbªp é
address pointed by pointer=Q╠╠╠╠£åbªp é
I have no idea why this is happening. Thanks in Advance.
The C++ library overloads the << operator for certain types. (char*) is one of them. Cout is trying to print a string, an array of characters terminated by a null character.
Just cast the pointer:
cout << "address pointed by pointer" << ( void* )char_pointer << endl;
or
cout << "address pointed by pointer" << static_cast<void*>(char_pointer) << endl;
The reason it prints out junky stuff is because your char does not have a null terminator which means the program will keep searching for one until, and in the process will print out whatever it finds. The text you see is ASCII but referenced by the address which the ostream is misinterpreting. To get the address held in memory, you could use implicit conversion or a static_cast. I prefer the latter:
cout << "address pointed by pointer=" << static_Cast<void*>(char_pointer) << endl;
Like 2501 said, in different wording, &c, since c is a char, equals a char *, so it's going to try to print until the new line character '\0' that is either implicitly or explicitly put in character arrays going to std::cout so the stream knows where the end of the character array is.
So, yeah use the (void *) like 2501 said.
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I have this code:
#include <iostream>
using namespace std;
int & squareRef(int );
int main() {
int number1 = 8;
cout << "In main() &number1: " << &number1 << endl;
int & result = squareRef(number1);
// cout << "In main() &result: " << &result << endl;
cout << result << endl;
cout << result << endl;
cout << number1 << endl;
}
int & squareRef(int rNumber) {
cout << "In squareRef(): " << &rNumber << endl;
rNumber *= rNumber;
return rNumber;
}
The program produces the following output:
In main() &number1: 0x28ff08
In squareRef(): 0x28fef0
64
1875681984
8
Can anyone please explain why the two "results" are different , is that suppose to be same isn't ?
You are invoking undefined behaviour by returning a reference to a local variable:
test.cc:19:7: error: reference to local variable ‘rNumber’ returned [-Werror=return-local-addr]
int & squareRef(int rNumber) {
rNumber is copied on the stack for the call. After the call, the value on the stack is undefined, and might well change due to subsequent calls. The reference you return only points to that location on the stack and does not hold the actual value.
In general, when these things happen, it is very helpful to turn on all warnings your compiler can give you. With gcc, the flags -Wall -Wextra -Werror provide you with a lot of helpful warnings, such as these. In general, code should compile without throwing any warnings (except maybe unused variables/parameters in function stubs, although there are macros to explicitly skip over these).
I observe some weird behavior today , the code is as follow :
The Code :
#include <iostream>
struct text
{
char c;
};
int main(void)
{
text experim = {'b'};
char * Cptr = &(experim.c);
std::cout << "The Value \t: " << *Cptr << std::endl ;
std::cout << "The Address \t: " << Cptr << std::endl ; //Print weird stuff
std::cout << "\n\n";
*Cptr = 'z'; //Attempt to change the value
std::cout << "The New Value \t: " << *Cptr <<std::endl ;
std::cout << "The Address \t: " << Cptr << std::endl ; //Weird address again
return 0;
}
The Question :
1.) The only question I have is why cout theAddress for the above code would come out some weird value ?
2.)Why I can still change the value of the member c by dereferenncing the pointer which has weird address ?
Thank you.
Consider fixing the code like this:
std::cout << "The Address \t: " << (void *)Cptr << std::endl ;
There's a std::ostream& operator<< (std::ostream& out, const char* s ); that takes a char* so you have to cast to void* to print an address, not a string it "points" to
I think the "weird" stuff shows up because cout thinks it's a cstring, i.e. a 0-terminated character array, so it doesn't print the address as you expected. And since your "string" isn't 0-terminated, all it can do is walk the memory until it encounters a 0. To sum it up, you're not actually printing the address.
Why I can still change the value of the member c by dereferenncing the
pointer which has weird address
The address isn't weird, as explained above. In your code Cptr points to a valid memory location and you can do pretty much anything you want with it.