简体   繁体   English

将指针作为函数的参数传递

[英]passing pointers as arguments to functions

I need help with understanding how pointers are working in this code below as i am really confused.The code is about finding maximum and the minimum value from a given array.I know that the problem can also be approached by using a structure but i want to understand use of pointers 我真的很困惑,下面我需要帮助理解指针如何在此代码中工作。代码是关于从给定数组中找到最大值和最小值。我知道也可以通过使用结构来解决问题,但是我想要了解指针的使用

#include<stdio.h>
int max2,min2,max,min,maximum,minimum;
int Maxmin(int a[],int *max,int *min,int i,int j)
{
    if(i==j)
    {
        return *max=*min=a[i];
    }
    else if(i==j-1)
    {
        if(a[i]<a[j])
        {
            *max=a[j];
            *min=a[i];
        }
        else
        {
            *max=a[i];
            *min=a[j];
        }
    }
    else
    {
        int mid=(i+j)/2;
        Maxmin(a,max,min,i,mid);
        Maxmin(a,&max2,&min2,mid+1,j);

    }
    if(*max<max2)
    {
        *max=max2;
    }

    if(*min>min2)
    {
        *min=min2;
    }

}

int main()
{
    int a[]={4,30,2,100,230};
    Maxmin(a,&max,&min,0,4);
    printf("%d %d",max,min);
}

From the caller side: 从呼叫方:

max and min are int variables. maxmin是int变量。 &max and &min are the memory addresses for these int variables. &max&min是这些int变量的内存地址。

From the callee side: 从被叫方:

int *max and int *min are declared as pointers to int in Maxmin . int *maxint *minMaxmin被声明为指向int的Maxmin Let's see it as if pointers contain a memory address. 让我们看一下,好像指针包含一个内存地址。 Pointers to int contain the memory address of an int variable. 指向int的指针包含一个int变量的内存地址。

*max and *min is the way to access the memory address content (dereferencing the pointer). *max*min是访问内存地址内容(取消引用指针)的方式。 Since you have a pointer to an int, you'll read an int out of that operation. 由于您有一个指向int的指针,因此您将从该操作中读取一个int。

The key to this code is to know that, if you modify *max or *min from within Maxmin , you will be changing the value of max and min outside the function, because you're working directly with the memory contents of those global variables. 此代码的关键是要知道,如果您在Maxmin内修改*max*min ,那么您将在函数外部更改maxmin的值,因为您直接使用这些全局变量的内存内容。

First of all you need to differentiate between * , & symbols used for pointers and their usage. 首先,您需要区分用于指针的*,&符号及其用法。

* operator Can be used in two ways *运算符可以以两种方式使用

  1. * as can be used to define a pointer Eg. * as可用于定义指针Eg。 int * ptr This thing will create a pointer and can store the address of type int int * ptr这东西将创建一个指针,并可以存储int类型的地址

  2. * as Dereference Operator printf("%d",*ptr); *作为取消引用运算符printf("%d",*ptr); prints the value who's address the pointer is storing. 打印指针存储的地址值。
    Eg. 例如。 int a = 10; int *ptr=&a; printf("%d",*ptr) ; printf("%d",*ptr) ; Dereference the value of pointer ans prints the value as 10 取消引用指针ans的值,将其打印为10

    & in Terms of pointers is used only as address of some variable. &就指针而言,仅用作某些变量的地址。

Now , In your code when you use *max and *min , it refers to the value of pointers used in function max , min , which ultimately refers to global variables as you have passed global variable's address in main function And when & is used in function for &max2 and &min2 it means address of global variables. 现在,在您的代码中,当您使用*max*min ,它指的是函数max,min中使用的指针的值,该指针最终指的是全局变量,因为您已经在主函数中传递了全局变量的地址,并且在&max2&min2函数表示全局变量的地址。

The code might be giving a wrong Output because its comparing the value of all the four global variables that can have a junk value . 该代码可能给出了错误的输出,因为它比较了可能具有垃圾值的所有四个全局变量的值。 So its better to initialize your global variables like this int max=-10000 ,min=10000 ,min2=10000 ,max2=-10000 . 所以最好像这样初始化全局变量int max=-10000 ,min=10000 ,min2=10000 ,max2=-10000 This will always give a correct output. 这将始终提供正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM