### Pointers help

I want to know how i can output pNum and how i can deference a pointer.

Any help is appreciated.

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; void inputDetails(int* n1, int* n2) { cout << "Enter two integers" << endl; cin >> *n1; cin >> *n2; } void outputDetails(int num1, int num2) { cout << "The value of your first number is " << num1 << endl; cout << "The value of your second number is " << num2 << endl; cout << "The address of your first number is " << &num1 << endl; cout << "The adress of your second number is " << &num2 << endl; } int main() { int num1, num2; int* pNum1 = &num1; int* pNum2 = &num2; inputDetails(pNum1, pNum2); outputDetails(num1, num2); cin.get(); cin.clear(); cin.ignore(numeric_limits::max(), '\n'); return 0; } ``````
Last edited on
 ``123456789101112131415161718192021`` ``````#include void input_details( int* n1, int* n2 ) { if( n1 != nullptr && n2 != nullptr ) { std::cout << "enter two integers: " ; std::cin >> *n1 >> *n2 ; } } int main() { int num1, num2 ; int* pnum1 = std::addressof(num1) ; // or: &num1 int* pnum2 = std::addressof(num2) ; input_details( pnum1, pnum2 ) ; // rest of the program }``````
Thank you! How would i be able to output pnum? I am able to output it in the main function, but i cant figure out how to output it in the outputDetails function.
assuming outputdetails is changed to refer to a pointer now (??)
you can just do
cout << pnum1[0]<< endl;

or (same result)
cout << *pnum1 << endl; // * syntax is confusing when doing math. You get *x * *y type statements. Yuck! I always use array [] notation for that reason.

and the pointer directly
cout << (unsigned int)(pnum) << endl; //the address itself.

if you did not change it to be pointers, it looks a lot like what you already did.

you have to be careful trying to see the address of something after moving it to a parameter. The address of the parameter may not be the address of the thing. I am not sure it would work to take &num in outputdetails to get the address of the arguments ... it may, but I don't remember if you can do that or not. It feels like that would quietly give the incorrect address but someone else can correct me if I am wrong.
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include void input_details( int* n1, int* n2 ) { if( n1 != nullptr && n2 != nullptr ) { std::cout << "enter two integers: " ; std::cin >> *n1 >> *n2 ; } } void output( const int& num1, const int& num2 ) // note: passed by reference to const { std::cout << "1. num1 value: " << num1 << '\n' << "2. num1 address in memory: " << std::addressof(num1) << '\n' << "3. num2 value: " << num2 << '\n' << "4. num2 address in memory: " << std::addressof(num2) << '\n' ; const int* pNum = std::addressof(num1) ; // or &num1 ; std::cout << "5. pNum value: " << pNum << '\n' << "6. pNum dereferenced value: " << *pNum << '\n' << "7. pNum address in memory: " << std::addressof(pNum) << '\n' ; } int main() { int num1, num2 ; input_details( std::addressof(num1), std::addressof(num2) ) ; output( num1, num2 ) ; }``````