代码:
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
int main()
{
time_t start, end;
time (&start);
int i;
double dif;
/*int serie[100000];*/
/* int *serie = malloc( sizeof(int) );
for (i = 0; i <= 100000; i++) {
*serie(i)=rand();
printf("%d \n", serie[i]);
}
*/
time (&end);
dif = difftime (end, start);
printf ("Time of execution is: %f\n", dif );
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
简介:(无需阅读)
我从大约3年前就知道了一些C++.我决定学习C来创建一个"快速"子集和算法.这是一个学习C的简单程序.问题是创建随机数和时间.当我使用Visual Studio编译为默认或C++时,此代码实际上有效,但我决定在C中执行此操作,并且我想创建一个动态数组.
似乎new在C中没有.我们必须使用malloc,但编译malloc我认为它必须在C中编译.在C++中,它给了我这个错误:
cannot convert from 'void …Run Code Online (Sandbox Code Playgroud) 我在C中有一个代码,其中包含以下行:
int i;
int *serie = malloc(sizeof(int));
for (i = 0; i <= 20; i++){
serie=rand();
printf("%d ",&serie[i]);/* *serie */
}
Run Code Online (Sandbox Code Playgroud)
它确实有用,但我想知道为什么,对于malloc,我相信我正在创建一个名为serie的动态数组或指针,到目前为止,我的知识是:
& returns the address
* returns the content of the adress
Run Code Online (Sandbox Code Playgroud)
使用固定数组[]和指针()
通过测试&serie[i]似乎工作,但它不*serie(i)还是*serie[i]和*serie我认为它不会太.
有人能解释一下这些吗?
如果我想打印内容不应该放*而不是&,我认为使用动态数组[]而不是()它应该*serie[i]不是&serie[i]吗?