首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言的程序,关于计算N随机点其间距离小于n的对数

2012-09-14 
C语言的程序,关于计算N随机点之间距离小于n的对数在指针这块出错point : illegal use of this type as a

C语言的程序,关于计算N随机点之间距离小于n的对数
在指针这块出错
'point' : illegal use of this type as an expression


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
typedef struct {float x;float y;} point ;
float randl(){return 1.0*rand()/(RAND_MAX+1) ;} ;
float dist(point p,point q){
  float dx = p.x-q.x;
float dy = p.y-q.y;
return sqrt(dx*dx + dy*dy) ;
}

int main(int argc,char * argv[])
{
int i,j,cnt=0,N=atoi(argv[1]) ;
float d = atof(argv[2]);
srand(time(NULL));
point *a = malloc(N*sizeof(*a)) ;
for(i=0;i<N;i++){
a[i].x = randl();
a[i].y = randl();
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
if(dist(a[i],a[j])<d) cnt++;
}
printf("%d egdes shorter than %f\n",cnt,d) ;
return 0;
}

[解决办法]
这是C还是C++?
C的话:
srand(time(NULL));
point *a = malloc(N*sizeof(*a)) ;

这两句换个位置。定义不能在这里定义。
point *a = malloc(N*sizeof(*a)) ;
srand(time(NULL));

另,请加个类型转换
point *a = (point *)malloc(N*sizeof(*a)) ;

希望能帮到你
[解决办法]
应该是1L提到的问题:

'point' : illegal use of this type as an expression
(不能将类型标示符"point"用作表达式)
换句话说,编译器没有把
point *a = malloc(N*sizeof(*a)) ;
当作声明,而是把它当作表达式处理了。

热点排行