how can i get address of char b?


1
2
3
4
5
6
7
8
9
10
11
12
13
 #include<iostream>
#include<string>
using namespace std;
int main(){
    int a  = 10 ; 
    cout<<"a is : "<< a <<endl<<"&a is : "<<&a;
    char b = 'f';
    string g = "d";
    cout<<endl <<"b is  : "<< b <<endl<<"&b is : "<<&b
    <<endl<<"g is  : "<<g <<endl<<"&g is : "<<&g<<endl;

return 0; 
}

output
-----------------
a is : 10
&a is : 0x7ffcdfea4c08
b is : f
&b is : f

g is : d
&g is : 0x7ffcdfea4be0
Last edited on
std::cout << &b attempts to print a NTBS (null terminated byte string, a sequence of characters in an array), where the address of the first character to be printed is given by &b.

To print out the actual address of a character, convert the pointer to char to pointer to const void; there is no special treatment for pointer to void, the value of the pointer is printed out.

1
2
3
4
5
6
7
8
#include <iostream>

int main() {

    char b = 'f';
    const void* pv = &b ; // char*  implicitly converted to const void*
    std::cout << "b is  : " << b << '\n' <<"address of b is : "<< pv << '\n' ;
}
To print out the actual address of a character, convert the pointer to char to pointer to const void


It should be :
To print out the actual address of a character, convert the pointer to char to pointer to const void*


Just a little bit correction.
> Just a little bit correction: convert the pointer to char to pointer to const void*

There is no implicit conversion from pointer to char to pointer to const void*
http://coliru.stacked-crooked.com/a/34d58eecaf0540b5

Repeat: To print out the actual address of a character, convert the pointer to char to pointer to const void
This is what the standard guarantees:
A prvalue of type “pointer to cv T ”, where T is an object type, can be converted to a prvalue of type “pointer to cv void ”. The result of converting a non-null pointer value of a pointer to object type to a “pointer to cv void ” represents the address of the same byte in memory as the original pointer value
Topic archived. No new replies allowed.