So I have the following (very simple) code:
int* pInt = new int(32);
std::cout<< pInt << std::endl; //statement A
std::cout<< *pInt << std::endl; //statement B
std::cout << &pInt << std::endl; //statement C
So here’s what I think I am doing (I have learned that in C++ I am rarely ever doing what I think I am doing):
- Creating a pointer to an integer and calling it pInt
- statementA prints the address of the value ’32’
- statementB prints the integer value that is being pointed to by my pointer (which is done because I dereference the pointer, thus giving me access to what it is pointing to).
- statementC prints the address of the pointer itself (not the address of the integer value ’32’).
Is all of this correct?
Your second statement is wrong. You allocate a new int off the heap. The compile-time constant “32” does not have an address, and therefore you cannot take it. You create an int whose value is 32. This is not the same thing.
pInt1 and pInt2 are not equal.
Oh, one last thing- C++ is a pretty strongly typed language, and it’s pretty unnecessary to prefix variable names with type data. This is called Hungarian Notation, it’s very common in Microsoft C libraries and samples, but generally unnecessary here.