Leetcode 812 C definition of pointers and arrays and array of pointers and pointer array assignment problem

About int ** points, as explained:
int * p is represented by a pointer indicating the address pointed to by p which is stored a value of type int. int ** p represents the two pointer indicating the address pointed to by p which is stored a pointer to a pointer of type int.

Storing a pointer address of a variable, the value pointed to the variable content. The
int * p = {1,2,3}, p = first address in the array, the first array value * p =;

Two pointer address storage a pointer, a pointer points to.
* p = {l, 2,3 int},
int ** pp = & p,
pp = first address pointer p, * pp = first address of the array, the array ** pp = 1 the first value.

Array name corresponds to an array of pointers to pass parameters i.e. two pointers to pointers, array name two-dimensional array of transfer parameters corresponding to a pointer to an array of one-dimensional arrays of pointers

The difference between two pointers and two-digit group consisting of:
a pointer over the parameter value can be changed in the address pointed to by the argument in. Argument can not modify the address pointer points to. It need only be means of two pointers.

Practical operation:
function call double ans = largestTriangleArea (points);
function definition largestTriangleArea Double (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 *'
	//指针数组里面存了数不是指针

We must be given the size of the second dimension
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 ** '

Two-dimensional array can not be directly used, involves a two-dimensional array of pointers:
C language pointer to an array of two-dimensional (two-dimensional array of pointers points) Detailed

[] Higher priority than * () must be added, if naked writing int * p [4], it should be understood as int * (p [4]), p becomes a pointer array, not a two-dimensional array pointer

C language pointer arrays (each array element is a pointer) Explanation
Note Sample

modify:

	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]};
	//也通过
	

An array of pointers to traverse only
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 ** '

Define an array, the array must be a value within the number of type int can not be assigned to location
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 *'

{} Is the use of {0,0} p1 address this array, there is no need & adding
the address of the array is located points1 five address; this address directly assigned to two pointer
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};
	//通过

5 of 6 get inside int ** points = points1; not to be the cause?
Array name corresponds to an array of pointers to pass parameters i.e. two pointers to pointers, array name two-dimensional array of transfer parameters corresponding to a pointer to an array of one-dimensional array of pointers. * points is an array of pointers to the corresponding reference address transfer addresses, i.e. two pointers, direct parameter passing.

complete

#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");
}
``

Released two original articles · won praise 0 · Views 36

Guess you like

Origin blog.csdn.net/mry_603/article/details/104996591