The contents of the code are as follows. As you can see below, when the parameters are double pointers, you hand over the factor I don't know why I have to put & on the factor pointer.
For example, if ptr is designated as a single pointer, printf("%p %p",ptr,&ptr); If you run , you can see that the same address value is displayed.
The pointer itself means the address value Is the reason for adding & just because it's against grammar?
----------------------------------------------------------
void maxandmin(int **max,int **min,int *arr)
{
char i;
int *maxp,*minp;
maxp = arr;
minp = arr;
for(i=1;i<5;i++)
{
maxp = *maxp > arr[i] ? maxp : &arr[i];
minp = *minp < arr[i] ? minp : &arr[i];
}
*max = maxp;
*min = minp;
return;
}
int main() {
int *maxptr;
int *minptr;
int arr[5] = { 3 , 4 , 5, 1, 2 };
maxandmin(&maxptr,&minptr,arr); // why not maxptr and add &?
printf("MAX = %d , MIN = %d ",*maxptr,*minptr);
return 0;
}
c
Pointers and double pointers have the same point that address values are stored, but they are clearly different data types.
This case is when a double pointer variable is used to implement a call-by-reference for the pointer variable.
If you understand the difference in the inverse reference result, you'll see why it's different.
inta=100;
int* p = &a;
int** d = &p;
*p is an inverse reference to the value of a. *d is an inverse reference to the value of p (address of a).
In other words, the double pointer variable d stores the address of p, which contains the address of a.
I'm confused about the double pointer because it has a double structure If you think about it using substitution, it's simple.
typedefint* PINT;
// // PINT* == int**
If int** replaces int* with PINT, you can view it as a pointer type of PINT. You can then change the code in the function circle as shown below.
void maxandmin (PINT* max, PINT* min, PINTarr)
{
char i;
PINT maxp, minp;
maxp = arr;
minp = arr;
for(i=1;i<5;i++)
{
maxp = *maxp > arr[i] ? maxp : &arr[i];
minp = *minp < arr[i] ? minp : &arr[i];
}
*max = maxp;
*min = minp;
return;
}
int main() {
PINT maxptr;
PINT minptr;
int arr[5] = { 3 , 4 , 5, 1, 2 };
maxandmin(&maxptr, &minptr,arr); // PINT's pointer type requires &
printf("MAX = %d , MIN = %d ",*maxptr,*minptr);
return 0;
}
© 2024 OneMinuteCode. All rights reserved.