给出飞机上的m个点数。 xy坐标的数量必须是 通过键盘输入。如何从xy找到这个坐标?用二维动态数组。
现在我有了这个,但它不起作用:
int **enterPoints (int m) {
int i, **points;
scanf("%d",&m);
points = (int **)malloc(m*sizeof(int *));
if (points != NULL) {
for (i=0; i<m; i++) {
*(points+i) = (int *)malloc(m*sizeof(int));
if (*(points+i)==NULL)
break;
}
{
printf("enter %d points coord X and Y:", i+1);
scanf("%d %d", &*(*(points+i)+0), &*(*(points+i)+1));
*(*(points+i)+2)=0;
}
}
free(points);
return points;
}
答案 0 :(得分:0)
试试这个
#include <stdio.h>
#include <stdlib.h>
int **enterPoints (int m){
int i, **points;
//scanf("%d",&m);//already get as argument
if(m<=0)
return NULL;
points = (int**)malloc(m*sizeof(int*));
if (points != NULL){
for (i=0; i<m; i++){
points[i] = (int*)malloc(2*sizeof(int));
if(points[i]!=NULL){
printf("enter %d points coord X and Y:", i+1);fflush(stdout);
scanf("%d %d", &points[i][0],&points[i][1]);
}
}
}
//free(points);//free'd regions is unusable
return points;
}
int main(void){
//test code
int i, m, **points;
//scanf("%d", &m);
m = 3;
points = enterPoints(m);
for(i = 0; i < m; ++i){
printf("(%d, %d)\n", points[i][0], points[i][1]);
free(points[i]);
}
free(points);
return 0;
}