1) int k=0; int *ptr = &k; int k=0; int *ptr = &k;

Can I artificially give a pointer an address to another memory location?

noticeably so ptr = 0x10bac6138 ;

2) How can I create a variable so that its visibility zone is in main.cpp and main.hpp ?

Closed due to the fact that the question is too general for the participants of VTT , αλεχολυτ , nick_n_a , 0xdb , aleksandr barakin 9 Mar at 5:20 pm .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Read about extern variables . - LLENN
  • 1) easily ptr = (int*)0x10bac6138; I will not write for risks. - nick_n_a
  • Two questions in one topic, unfortunately, clog up the resource and complicate the search for it. Divide the questions into two topics. - gbg
  • 2) if according to the rules - the advertisement from hpp will be visible in cpp, but at the same time hpp must be connected to cpp. If not connected, connect via include. Also - read the basics with - nick_n_a

1 answer 1

It is possible to refer to an arbitrary address in the memory, but you should know for sure that there is something at this address.

That is, this address is obtained either as part of the address block returned by the memory allocation function, or obtained from the operating system. Or you saw it in the directory for the hardware that executes your program.

If the address you wrote at random, it is very likely that when dereferencing such a pointer, or accessing it, the program will behave unpredictably.