Importance of this specific piece of code in producing the correct result?

Why is the piece of code: " getSeconds(&sec);" necessary to produce the correct result?

With the following code, it produces a number:
Number of seconds :1294450468

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
#include <iostream>
#include <ctime>
 
using namespace std;
void getSeconds(unsigned long *par);

int main ()
{
   unsigned long sec;


   getSeconds( &sec );

   // print the actual value
   cout << "Number of seconds :" << sec << endl;

   return 0;
}

void getSeconds(unsigned long *par)
{
   // get the current number of seconds
   *par = time( NULL );
   return;
}


However, without the piece of code: " getSeconds(&sec);" it produces a constant:
Number of seconds: 4285854

The latter result is obviously not the address, but I also don't know what it is, and why this constant appears when the mentioned piece of code is deleted.
time(NULL) returns the current time.

If you don't use that, than your unsigned long sec; variable doesn't have a value. You declared it, but you didn't give it some value. In this case, you get some strange values, which comes from memory.
(Try it out, with other variables: declare it and than write out without giving it a value.)
Topic archived. No new replies allowed.