0

I have some code where norms is declared as a double array of size 3 x 2. I would like to output norms[0] to the console.

#include <iostream> 
using namespace std;
    
double norms[3][2];
_cal_norm(dept,size,max_posn,norms[0]);
cout << norms[0];

I would like to have the double value returned. I have tried using cout << norms[0] to return it. But it returns the following instead:

norms[0] 1 0x7fff33d52180

Is there a way for me to see the double value?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Stacey
  • 4,825
  • 17
  • 58
  • 99
  • 1
    If you want to print the two values in the `norms[0]` subarray, then print them both: `std::cout << norms[0][0] << " " << norms[0][1] << std::endl;` Otherwise, you're asking to print the *address* of the subarray. – Adrian Mole Jul 28 '20 at 20:56
  • I think you mean `norms[0][0]`. `norms` is a two-dimensional array; `norms[0]` is a one-dimensional array. – einpoklum Jul 28 '20 at 20:56
  • You need a loop of some sort. Related: [https://stackoverflow.com/questions/12311149/how-to-print-2d-arrays-in-c](https://stackoverflow.com/questions/12311149/how-to-print-2d-arrays-in-c) – drescherjm Jul 28 '20 at 21:05
  • 3
    Unrelated, but all identifiers that are in the global namespace with leading underscores are reserved, and using such a name has undefined behavior. You should _not_ call your function _cal_norm(). – Chris Uzdavinis Jul 28 '20 at 21:12

1 Answers1

5

norms is a 2-dimensional array of arrays, so norms[0] is the 1st inner array in the outer array. If you use cout << norms[0];, it will print the address of that first array. That's why you got a hexadecimal number. If your goal is to print a number in a 2D array then you should write cout << norms[0][0]; which will print out the 1st value of the 1st array in norms, for example.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
PopJoestar
  • 462
  • 1
  • 4
  • 10