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 96 views
96 views asked Nov 4, 2022 by avibootz
1 answer 149 views
1 answer 154 views
154 views asked Nov 4, 2022 by avibootz
1 answer 131 views
1 answer 220 views
1 answer 191 views
...