为什么我们使用 'NULL'?

Why do we use 'NULL'?

为什么我们在下面的代码中使用 'NULL'。

为什么我们不能将种子乘以一个整数?

抱歉,我是 C++ 新手。

代码

srand(time(NULL));

time函数可以将时间写入函数调用指针提供的位置。这个指针参数可以是空指针,然后time只returns当前时间。

在大多数系统上,time 函数 returns 自纪元以来的秒数,因此是用于 seeding the random number generator.[=18= 的非常独特的整数值]


单一语句

srand(time(NULL));

相当于

time_t temp_time = time(NULL);
srand(temp_time);

或者如果我们想使用非空指针

time_t temp_time;
time(&temp_time);
srand(temp_time);