关于int** points, 讲解为:
int *p表示的是一级指针,表示p所指向的地址里面存放的是一个int类型的值。int **p表示的是二级指针,表示p所指向的地址里面存放的是一个指向int类型的指针。
一级指针存放变量的地址,指向的值是变量的内容。如
int* p={1,2,3}, p=数组的首地址,*p=数组的第一个值;
二级指针存放一级指针的地址,指向一级指针。
int*p ={1,2,3},
int**pp=&p,
pp=指针p的首地址,*pp=数组的首地址,**pp=数组第一个值1。
指针数组的数组名传递参数对应为指针的指针即二级指针,二维数组的数组名传递参数对应为数组指针即指向一维数组的指针
二级指针和二位数组的区别:
一级指针通过形参,可以修改实参中指针所指向的地址中的值。修改不了实参中指针所指向的地址。需要借助二级指针才可以。
实操:
函数调用double ans=largestTriangleArea(points);
函数定义double largestTriangleArea(int** points){}
1:
int points[][2]={{0,0},{0,1},{1,0},{0,2},{2,0}};//传参报错cannot convert parameter 1 from 'int [5][2]' to 'int ** '
int *points[][2]={{0,0},{0,1},{1,0},{0,2},{2,0}};//cannot convert from 'const int' to 'int *'
//指针数组里面存了数不是指针
必须给出第二维的大小
2:
int points1[][2]= {{0,0},{0,1},{1,0},{0,2},{2,0}};
int **points=&points1;//报错cannot convert from 'int (*)[5][2]' to 'int ** '
int **points=points1;//cannot convert from 'int [5][2]' to 'int ** '
不能直接用二维数组,涉及到二维数组指针:
C语言二维数组指针(指向二维数组的指针)详解
[ ]的优先级高于*,( )是必须要加的,如果赤裸裸地写作int *p[4],那么应该理解为int *(p[4]),p 就成了一个指针数组,而不是二维数组指针
修改:
int points1[5][2]= {{0,0},{0,1},{1,0},{0,2},{2,0}};
int *points2[5]={points1[0],points1[1],points1[2],points1[3],points1[4]};
//可以用sizeof(points1[0])/sizeof(points1[0][0])写成遍历得到所有地址
int **points=points2;//通过
int *points[5]={points1[0],points1[1],points1[2],points1[3],points1[4]};
//也通过
只能遍历出指针数组
3:
int p1[]={0,0}; int p2[]={0,1}; int p3[]={1,0}; int p4[]={0,2}; int p5[]={2,0};
int points1[]={p1,p2,p3,p4,p5};//cannot convert from 'int [2]' to 'int'
int **points=&points1;//cannot convert from 'int (*)[1]' to 'int ** '
定义数组,数组内的值必为int类型的数,不能赋地址
4:
int p1[]={0,0}; int p2[]={0,1}; int p3[]={1,0}; int p4[]={0,2}; int p5[]={2,0};
int* points1[]={&p1,&p2,&p3,&p4,&p5};//cannot convert from 'int (*)[2]' to 'int *'
{}内用的p1是{0,0}这个数组的地址,所以不需要加&
而points1是存放5个地址的数组的地址;这个地址直接赋给二级指针
5:
int p1[]={0,0}; int p2[]={0,1}; int p3[]={1,0}; int p4[]={0,2}; int p5[]={2,0};
int *points1[]={p1,p2,p3,p4,p5};
int **points=*points1;//cannot convert from 'int *' to 'int ** '
int **points=points1;//通过
int **points=&points1;//cannot convert from 'int *(*)[5]' to 'int ** '
6:
int p1[]={0,0}; int p2[]={0,1}; int p3[]={1,0}; int p4[]={0,2}; int p5[]={2,0};
int * points[]={p1,p2,p3,p4,p5};
//通过
由6得5里面的int **points=points1;不必须原因?
指针数组的数组名传递参数对应为指针的指针即二级指针,二维数组的数组名传递参数对应为数组指针即指向一维数组的指针。*points 是一个指针数组,传参对应为地址的地址,即二级指针,可直接传参。
完整
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
double largestTriangleArea(int** points, int pointsSize, int* pointsColSize){
double ans=0;
double a=0;
double b=0;
double c=0;
double p=0;
double ans1=0;
for(int i=0;i<pointsSize-1;i++)
for(int j=i+1;j<pointsSize;j++)
for(int k=0;k<pointsSize;k++)
{
if(k!=i&&k!=j)
{
a=sqrt((points[i][0]-points[j][0])*(points[i][0]-points[j][0])+(points[i][1]-points[j][1])*(points[i][1]-points[j][1]));
b=sqrt((points[i][0]-points[k][0])*(points[i][0]-points[k][0])+(points[i][1]-points[k][1])*(points[i][1]-points[k][1]));
c=sqrt((points[k][0]-points[j][0])*(points[k][0]-points[j][0])+(points[k][1]-points[j][1])*(points[k][1]-points[j][1]));
p=(a+b+c)/2;
ans1=sqrt(p*(p-a)*(p-b)*(p-c));
if(ans1>ans)
ans=ans1;
}
}
return ans;
}
void main()
{
// int *ans=(int *)malloc(sizeof(int)*(right-left+1));
int p1[]={0,0};
int p2[]={0,1};
int p3[]={1,0};
int p4[]={0,2};
int p5[]={2,0};
int *points[]={p1,p2,p3,p4,p5};
int pointsSize=sizeof(points)/sizeof(points[0]);
int pointsColSize[]={sizeof(p1)/sizeof(p1[0])};
double ans=largestTriangleArea(points, pointsSize, pointsColSize);
printf("%f\n", ans);
printf("%d\n", pointsSize);
printf("%d\n", pointsColSize[0]);
// for(int i=0;i<k;i++)
//printf("%d\n", ans[i]);
//if(ans) printf("true\n"); else printf("false\n");
}
``