C++ print value of a pointer
Asked Answered
X

4

6

I have an array of double pointers, but every time I try do print one of the values the address gets printed. How do I print the actual value?

cout << arr[i] ? cout << &arr[i] ? they both print the address

Does anyone know?

Xever answered 21/3, 2010 at 2:3 Comment(0)
D
12

If it's really an array of (initialized) double pointers, i.e.:

double *arr[] = ...
// Initialize individual values

all you need is:

cout << *arr[i];
Disc answered 21/3, 2010 at 2:4 Comment(0)
O
1

cout << *(arr[i]) will print the value.

Oshaughnessy answered 21/3, 2010 at 2:5 Comment(0)
B
1

cout << *(arr[i]);

Bright answered 21/3, 2010 at 2:5 Comment(3)
@Potatoswatter. The compiler might not, and technically neither do I, because I can remember the operator precedence if I have to. But they certainly improve things.Intransitive
@Potatoswatter: parens aren't needed for 5 + 112 % 65 ^ 1 / 5.5 && bar || foo^2 << 5 either, but (((5 + (112 % 65)) ^ (1 / 5.5)) && bar) || (foo^(2 << 5)) is much more clearBright
+1 for parenthesis, I always parenthesise that kind of expression.Johnnajohnnie
A
0

If "arr" is declared as

double* arr[..];

Then you would use:

cout << *(arr[i])
Argil answered 21/3, 2010 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.