求助:关于动态分配地址空间问题
#include<stdio.h>#include<stdlib.h>
int
tt (float (*p)[2], int a, int b, int N)
{
int *temp = (int *) malloc (sizeof (int) * N);
int k = 0;
int t;
int i, j;
for (i = 0; i < N; i++)
{
if (i == a || i == b)
{
continue;
}
t =
p[i][0] * (p[b][1] - p[a][1]) + (p[a][0] - p[b][0]) * p[i][1] -
p[a][0] * p[b][1] + p[b][0] * p[a][1];
if (t == 0)
return 0;
else if (t > 0)
temp[k++] = 1;
else
temp[k++] = -1;
}
for (j = 1; j < k; j++)
{
if (temp[0] != temp[j])
return 0;
}
return 1;
}
int
real (float p[][2], int n)
{
int *flag = (int *) malloc (sizeof (int) * n);
int m, i, j;
m = 0;
for (i = 0; i < n; i++)
{
flag[n] = 0;
}
for (i = 0; i < n; i++)
{
for (j = 1; j < n; j++)
{
if (flag[j])
continue;
if (tt (p, m, j, n))
{
flag[m] = 1;
m = j;
break;
}
}
}
flag[m] = 1;
for (i = 0; i < n; i++)
{
if (flag[i] == 0)
return 0;
}
if (tt (p, 0, m, n))
return 1;
return 0;
}
int
main ()
{
float (*p)[2];
int *space;
int N, i;
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
printf ("输入点的个数:");
scanf ("%d", &N);
p = (float *) malloc (sizeof (float) * N * 2);
printf ("输入坐标(例:3 4):");
for (i = 0; i < N; i++)
{
printf (" %d:", i);
/*scanf ("%f", &(*(p + i))[0]);*/
scanf ("%f", &p[i][0]);
scanf ("%f", &(*(p + i))[1]);
printf ("(%f , ", p[i][0]);
printf ("%f)\n", p[i][1]);
}
if (real (p, N))
printf ("可以构成凸多边形\n");
else
printf ("不能构成凸多边形\n");
printf ("是否继续<y\n>?");
scanf("%s",&ch);
}
return 0;
}
错误:
Compiling...
Cpp1.cpp
C:\Documents and Settings\JSZX\桌面\Cpp1.cpp(19) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
C:\Documents and Settings\JSZX\桌面\Cpp1.cpp(85) : error C2440: '=' : cannot convert from 'float *' to 'float (*)[2]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
Cpp1.obj - 1 error(s), 1 warning(s)