Pointers help

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

Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
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<streamsize>::max(), '\n');
	return 0;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

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 ) ;
}
Topic archived. No new replies allowed.