How to get the address of a variable in C++

2 Answers

0 votes
#include <iostream>

int main() {
    int n = 9;
     
    int* address_of_n = &n;
     
    std::cout << address_of_n << "\n";
    std::cout << &n;
}




/*
run:

0x7ffc21d4f88c
0x7ffc21d4f88c

*/

 



answered Nov 4, 2022 by avibootz
edited Nov 4, 2022 by avibootz
0 votes
#include <iostream>
#include <memory>

int main() {
    int n = 9;
      
    int* address_of_n = std::addressof(n);
      
    std::cout << address_of_n << "\n";
    std::cout << &n;
}
 
 
 
 
/*
run:
 
0x7ffcc40acb3c
0x7ffcc40acb3c
 
*/

 



answered Nov 4, 2022 by avibootz

Related questions

1 answer 102 views
102 views asked Nov 4, 2022 by avibootz
1 answer 154 views
1 answer 160 views
160 views asked Nov 4, 2022 by avibootz
1 answer 136 views
1 answer 229 views
1 answer 200 views
...