pointers and addresses??

I am writing a program to set a pointer double *d, to point to 3 variables (double d1,d2 d3). The values of the 3 variables are d1 = 7.8, d2 = 10.0 and d3 = .009.

I need to set the pointer d, to point to each of the variables and also print the value and address of each variable using d.

Second part of my problem is I need to then set the pointer to a 'double pointer' dp, to point to d. Then use dp to print the address and content of each double variable.

As far my code is as follows: It seems like there should only be one point *d, not *d_1, *d_2, *d_3. Also I have no idea as to how to print the'address' of each variable....?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main ()
{
  double d1, d2, d3;
  double * d_1, d_2, d_3;

//assigning the values to integer pointed to
  d_1= 7.8;
  *d_1 = 7.8;
  d_2 = &d2;
  *d_2 = 10.0;
  d_3 = &d3;
  *d_3 = 10.0;

//Print statements
  cout << "The value of d1 is " << *d_1 << endl;
  cout << " The value of d2 is " << *d_2 << endl;
  cout << " The value of d3 is " << *d_3 << endl;

  return 0;
}


** I am supposed to be using one pointer to point to different 'places'. but printing out the address and contents for each place that was pointed too....
Last edited on
well, i never heard making your pointer point to more than one memory location.
IMO, this might not be possible...
so, to get your desired output you may try

double* d_1, d_2, d_3 ; // i.e; declaring all of them as pointers

and in order to get the address, you may then use d_1 , d_2, d_3 simply ( after making them point to d1, d2, d3 respectively )
Write this:
1
2
3
4
5
cout << "The address of d_1 is " << d_1 << endl;
cout << "The address of d_2 is " << d_2 << endl;
cout << "The address of d_3 is " << d_3 << endl;
cout << "The address of d2 is " << &d2 << endl;
cout << "The address of d3 is " << &d3 << endl;


this will help :)
http://www.cplusplus.com/doc/tutorial/pointers/

closed account (z05DSL3A)
Suja Kushwaha wrote:
so, to get your desired output you may try
double* d_1, d_2, d_3 ; // i.e; declaring all of them as pointers

That only declares d_1 as a pointer.

double *d_1, *d_2, *d_3 ;
or better still only declare one variable per line to remove doubt;
1
2
3
double *d_1; 
double *d_2; 
double *d_3 ;
I have already altered my code to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main ()
{
  double d1, d2, d3;
  double * d;

  d= &d1;
  *d = 7.8;
  d = &d2;
  *d = 10.0;
  d=&d3;
  *d = .009;
  
  cout << "d1 value is " << d1 << endl;
  cout << "d2 value is " << d2 << endl;
  cout << "d3 value is " << d3 << endl;
  
  return 0;
}


Due to my limited knowledge of pointers I am not sure if I am coding it as required (see above post). Also, I still have to make it print the address of each as well.....????
Andmake a double pointer, dp, that pointers to the double pointer *d and prints that content and address....?????
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    double value = 0;
    double * pValue = &value;
    
    *pValue = 10.5;
    
    std::cout << "the value of value is " << *pValue << " the address is " << pValue << std::endl;
    
    double ** ppValue = &pValue;
    
    std::cout << "the value of value is " << **ppValue << " the address is " << *ppValue << std::endl;
    
    std::cout << "the value of pvalue is " << pValue << " the address is " << &pValue << std::endl;

    std::cout << "the value of pvalue is " << *ppValue << " the address is " << ppValue << std::endl;
    
    return 0;
}
the value of value is 10.5 the address is 0x7fff5fbff920
the value of value is 10.5 the address is 0x7fff5fbff920
the value of pvalue is 0x7fff5fbff920 the address is 0x7fff5fbff918
the value of pvalue is 0x7fff5fbff920 the address is 0x7fff5fbff918
Last edited on
Thanks for your help on this. Here is the program so far (I kept the pointer and values as d and d1, d2, d3 (as required) in the assignment:

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
#include <iostream>
using namespace std;

int main ()
{
  double d1, d2, d3;
  double * d;

  d= &d1;
  *d = 7.8;
  d = &d2;
  *d = 10.0;
  d=&d3;
  *d = .009;
  
  cout << "d1 value is " << d1 << endl;
  cout << "Address of d1 is: " << &d1  << endl;
  cout << "d2 value is " << d2 << endl;
  cout << "Address of d2 is: " << &d2  << endl;
  cout << "d3 value is " << d3 << endl;
  cout << "Address of d3 is: " << &d3  << endl;
  
  return 0;
}

Results:
d1 value is 7.8
Address of d1 is: 0x7fff257b7a30
d2 value is 10
Address of d2 is: 0x7fff257b7a28
d3 value is 0.009
Address of d3 is: 0x7fff257b7a20


So to make a double pointer, dp, that points to the double pointer, d and prints the contents and address would be?

 
 double **dp = &d;  //pointer declaration ? And this would be placed after all the cout statements of d1 through d3? 


Grey Wolf: Iam confused about the first cout after the double **ppValue = &pValue;

Is that the code to print out the value variable by using the double pointer, pointing to a double pointer? Wow that sounds confusing :) I am just curious, because in my case where I have 3 values how would I code that (your example just shows it for one value.

Thanks.
This is how I added the pointer pointing to a pointer....

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
#include <iostream>
using namespace std;

int main ()
{
  double d1, d2, d3;
  double * d;

  d= &d1;
  *d = 7.8;
  d = &d2;
  *d = 10.0;
  d=&d3;
  *d = .009;
  
  cout << "d1 value is " << d1 << endl;
  cout << "Address of d1 is: " << &d1  << endl;
  cout << "d2 value is " << d2 << endl;
  cout << "Address of d2 is: " << &d2  << endl;
  cout << "d3 value is " << d3 << endl;
  cout << "Address of d3 is: " << &d3  << endl;
  
  double **dp = &d;
  
  cout << "The value of d is " << d << " and the address is " << &d << endl; 
  cout << "The value of d is " << *dp << " the address is " << dp << endl;
  return 0;
}


Results:

d1 value is 7.8
Address of d1 is: 0x7fffee549ed0
d2 value is 10
Address of d2 is: 0x7fffee549ec8
d3 value is 0.009
Address of d3 is: 0x7fffee549ec0
The value of d is 0x7fffee549ec0 and the address is 0x7fffee549eb8
The value of d is 0x7fffee549ec0 the address is 0x7fffee549eb8

Does that look right??

Thanks.
closed account (z05DSL3A)
First it would be best to get out of the habit of calling it a double pointer it will confuse people.

double * is a pointer to a double, not a double pointer.
double ** is a pointer to a pointer to a double, not a double pointer...

Is that the code to print out the value variable by using the double pointer, pointing to a double pointer?

So, ppValue is a pointer to a pointer to the value object, in the line of code the * indicates that the point is dereferenced (you get what it points to). so the first * gets what ppValue points to, pValue. the second * gets what this pointer points to, value.

in my case where I have 3 values how would I code that

A pointer can only point to one object at a time, you would have to print the data you want re-assign the pointer to point to another object and so on.
Okay. I understand how that can be confusing. I was just repeating how it was worded in the assignment.which I guess is part of why I myself am confused.This is hopefully the finalized code for this assignment. If anything looks off or wrong please let me know...... Thanks!

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
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

int main ()
{
  double d1, d2, d3;
  double * d;

  d= &d1;
  *d = 7.8;
  d = &d2;
  *d = 10.0;
  d=&d3;
  *d = .009;
  
  cout << "d1 value is " << d1 << endl;
  cout << "Address of d1 is: " << &d1  << endl;
  cout << "d2 value is " << d2 << endl;
  cout << "Address of d2 is: " << &d2  << endl;
  cout << "d3 value is " << d3 << endl;
  cout << "Address of d3 is: " << &d3  << endl;
  
  double **dp = &d;
  
  cout << "The value of d is " << d << " and the address is " << &d << endl; 
  cout << "The value of d is " << *dp << " the address is " << dp << endl;
  
  d= &d1;
  dp =&d;
  
  cout << "The value of d1 is " << **dp << " and the address is " << &dp << endl; 
  
  d = &d2;
  dp =&d;
  
  cout << "The value of d2 is " << **dp << " and the address is " << &dp << endl;
  
  d = &d3;
  dp =&d;
  
  cout << "The value of d3 is " << **dp << " and the address is " << &dp << endl;
  
  return 0;
}
@Grey Wolf..

" That only declares d_1 as a pointer.

double *d_1, *d_2, *d_3 ;
or better still only declare one variable per line to remove doubt;
1.double *d_1;
2.double *d_2;
3.double *d_3 ;
"

well, i wrote double* d_1, d_2, d_3 ;
// this declares all of them as pointers.
if i would have wrote,
double * d_1, d_2, d_3
// This would have declared only d_1 as a pointer and rest as double variables.
Last edited on
closed account (z05DSL3A)
Suja Kushwaha wrote:
well, i wrote double* d_1, d_2, d_3 ;
// this declares all of them as pointers.

Really? Are you sure?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main()
{
    double value = 5.5;
    double* p1, p2, p3;
    
    p1 = &value;
    p2 = &value;
    p3 = &value;
    
    std::cout << *p1 << " " << *p2 << " " << *p3 << std::endl;
    return 0;
}

http://ideone.com/9yKod5
Last edited on
@Grey Wolf..

yeah.
sorry that was a misconception i had.

Thanks!!
:D
:D
Topic archived. No new replies allowed.