c语言中将函数指针作为形参_在C中将指针作为函数参数

c语言中将函数指针作为形参

Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. When a function is called by reference any change made to the reference variable will effect the original variable.

指针作为函数参数,用于保存函数调用期间传递的参数的地址。 这也称为按引用调用 。 通过引用调用函数时,对引用变量所做的任何更改都会影响原始变量。

时间示例:使用指针交换两个数字 (Example Time: Swapping two numbers using Pointer)

#include <stdio.h>

void swap(int *a, int *b);

int main()
{
    int m = 10, n = 20;
    printf("m = %d\n", m);
    printf("n = %d\n\n", n);

    swap(&m, &n);    //passing address of m and n to the swap function
    printf("After Swapping:\n\n");
    printf("m = %d\n", m);
    printf("n = %d", n);
    return 0;
}

/*
    pointer 'a' and 'b' holds and 
    points to the address of 'm' and 'n'
*/
void swap(int *a, int *b) 
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

m = 10 n = 20 After Swapping: m = 20 n = 10

m = 10 n = 20交换后:m = 20 n = 10

返回指针变量的函数 (Functions returning Pointer variables)

A function can also return a pointer to the calling function. In this case you must be careful, because local variables of function doesn't live outside the function. They have scope only inside the function. Hence if you return a pointer connected to a local variable, that pointer will be pointing to nothing when the function ends.

函数也可以return指向调用函数的指针。 在这种情况下,您必须要小心,因为函数的局部变量不在函数外部。 它们仅在函数内部具有作用域。 因此,如果您返回一个连接到局部变量的指针,则该函数结束时该指针将不指向任何内容。

#include <stdio.h>

int* larger(int*, int*);

void main()
{
    int a = 15;
    int b = 92;
    int *p;
    p = larger(&a, &b);
    printf("%d is larger",*p);
}

int* larger(int *x, int *y)
{
    if(*x > *y)
        return x;
    else
        return y;
}

92 is larger

92大

返回有效指针的安全方法。 (Safe ways to return a valid Pointer.)

  1. Either use argument with functions. Because argument passed to the functions are declared inside the calling function, hence they will live outside the function as well.

    参数与函数一起使用。 由于传递给函数的参数是在调用函数内部声明的,因此它们也将驻留在函数外部。



  2. Or, use static local variables inside the function and return them. As static variables have a lifetime until the main() function exits, therefore they will be available througout the program.

    或者,在函数内部使用static 局部变量并返回它们。 由于静态变量的生存期一直到main()函数退出,因此它们将在整个程序中可用。

功能指针 (Pointer to functions)

It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows,

可以声明一个指向某个函数的指针,然后将该指针用作另一个函数的参数。 指向函数的指针声明如下:

type (*pointer-name)(parameter);

Here is an example :

这是一个例子:

int (*sum)();   //legal declaration of pointer to function
int *sum();     //This is not a declaration of pointer to function

A function pointer can point to a specific function when it is assigned the name of that function.

当为函数指针分配了特定函数的名称时,它可以指向该函数。

int sum(int, int);
int (*s)(int, int);
s = sum;

Here s is a pointer to a function sum. Now sum can be called using function pointer s along with providing the required argument values.

s是指向函数sum的指针。 现在可以使用函数指针s以及提供所需的参数值来调用sum

s (10, 20);

功能指针的例子 (Example of Pointer to Function)

#include <stdio.h>

int sum(int x, int y)
{
    return x+y;
}

int main( )
{
    int (*fp)(int, int);
    fp = sum;
    int s = fp(10, 15);
    printf("Sum is %d", s);

    return 0;
}

25

25

复杂功能指针示例 (Complicated Function Pointer example)

You will find a lot of complex function pointer examples around, lets see one such example and try to understand it.

您会发现很多复杂的函数指针示例,让我们看一个这样的示例并尝试理解它。

void *(*foo) (int*);

It appears complex but it is very simple. In this case (*foo) is a pointer to the function, whose argument is of int* type and return type is void*.

它看起来很复杂,但是非常简单。 在这种情况下(*foo)是指向函数的指针,该函数的参数为int*类型,返回类型为void*

翻译自: https://www.studytonight.com/c/pointer-with-function-in-c.php

c语言中将函数指针作为形参

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页