gpt4 book ai didi

c - 什么是函数指针?

转载 作者:行者123 更新时间:2023-11-30 20:06:39 26 4
gpt4 key购买 nike

用简单的语言来说,什么是函数指针?

最佳答案

用简单的英语来说,

A FUNCTION_POINTER is a pointer which points towards the address of fuction's first instruction, like a POINTER which points towards the address of a variable.

举一个程序示例来理解这个概念

Sum of all integers upto the user i/p

-

#include <stdio.h>

int IsAny(long n)
{
return 1;
}

long AddIf(long limit, int (*funPointer)(long))
{
long sum = 0;
register int i;

for(i = 1; i <= limit; ++i)
{
if(funPointer(i))
sum += i;
}

return sum;
}

int main(void)
{
long l, total;

printf("Enter a positive integer: ");
scanf("%ld", &l);

total = AddIf(l, IsAny);
printf("Sum of all integers upto %ld is %ld\n", l, total);
}

此处调用 FUNCTION_POINTER 以通过声明调用 AddIf 中的 IsAny 函数 作为 AddIf 函数中的 int (*funPointer)(long))

关于c - 什么是函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22678151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com