Pointer address and address of variables to which pointer points [duplicate] - c++

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

Related

Disproving Determinism via c++ [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
C/C++ changing the value of a const
(18 answers)
Closed 5 months ago.
friends.
Recently I experimented a bit a C++ constants.
The code is:
#include <iostream>
int main() {
const int c = 1;
const int* ptr = &c;
int* tmp = const_cast<int*>(ptr);
*tmp = 5;
std::cout << &c << " " << ptr << " " << tmp << "\n";
std::cout << c << " " << *ptr << " " << *tmp;
}
I have investigated assembly code at godbolt: https://godbolt.org/z/7e3o7bWrs
The assembly code seems like doing what I wrote, exactly moving address of c into tmp variable and changes variable at this address.
Can you please tell me, why there is could be two different values at the same addresses?
Thank you.

Pointer Syntax Within Condition

After a pointer is initialized, do you have to use the * dereference operator to call the pointer in a condition?
Example:
int main()
{
int var = 10;
int *ptr = &var;
if(ptr) // does this need to be if(*ptr) ???
{.......}
}
And can I have a short explanation as to why?
Thank you.
if (ptr)
check if the pointer is not Null but
if (*ptr)
check if the value it points to is not zero (in this example is 10)
So for checking the value you shoud add *.
It depends on what you want to do.
if(ptr) checks if the pointer value is nullptr or not. Note that this is shorthand for if(ptr != nullptr).
if(*ptr) checks if what the pointer points to is nullptr (or 0) - and in that case, since you dereference (follow) the pointer to answer the question, the pointer itself had better not be nullptr in that case.
First of all, a pointer is only a variable. However, there are different contexts in which you can use it.
As any other variable you can access the pointers content (which is the adress of the underlying memory) as follows:
int i = 1;
int * p = &i;
std::cout << p << std::endl
this would output the adress of i since this is what is stored in p
If you however want to access the content of the underlying memory (the value of i), you need to dereference the pointer first by using the * operator:
std::cout << *p << std::endl;
This would print the value of iso 1.
of course you can also access the pointer's adress (since the adress of i is a numeric value as well and needs to be stored somewhere too):
std::cout << &p << std::endl;
That would output the adress of p so the adress where the adress of i is stored :)
As a little example try to run this code:
#include <iostream>
int main() {
int i = 1;
int * p = &i;
std::cout << "Value of i(i): " << i << std::endl
<< "Adress of i(&i): " << &i << std::endl
<< "Value of p(p): " << p << std::endl
<< "Dereferenced p(*p): " << *p << std::endl
<< "Adress of p(&p): " << &p << std::endl
<< "Dereferenced adress of p(*(&p)): " << *(&p) << std::endl;
}

variable value and its address using pointers in C++

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.

C++ setting char pointer to null [duplicate]

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);

Weird Pointer Address for Individual Struct Data Member

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.