This question already has an answer here:
这个问题在这里已有答案:
- Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'? 2 answers
为什么函数指针定义适用于任意数量的&符号'&'或星号'''? 2个答案
My first doubt is:
我的第一个疑问是:
Is the name of function a pointer variable and Which variable's address is the pointer holding ? (in this program which variable's address addition pointer holds).
函数的名称是否为指针变量,哪个变量的地址是指针保持? (在这个程序中变量的地址添加指针成立)。
AND if i write '&' before addition, program runs fine. Is compiler automatically add '&' during compilation ???
如果我在添加之前写'&',程序运行正常。编译时编译器会自动添加'&'吗?
#include<stdio.h>
int addition(int, int);
int main()
{
int (*p)(int, int);
int sum;
p=addition; // if I add '&' before function name the
//program runs fine. (&addition).
sum=p(10, 20);
printf("Sum is %d\n", sum);
return 0;
}
int addition(int x, int y)
{
int r;
r=x+y;
return r;
}
Output is: Sum is 30.
输出为:Sum为30。
4 个解决方案
#1
2
int (*p)(int, int);
int(* p)(int,int);
Here you are declaring a pointer to a function.
在这里,您将声明一个指向函数的指针。
In p = addition;
You are pointing p
to addition
function. Just as variables which are declared have addresses, functions too have their own addresses. So, by doing that you are assigning the address of addition
to p
and thus p
points to addition
function.
在p =加法;您指向p添加功能。就像声明的变量具有地址一样,函数也有自己的地址。因此,通过这样做,您将为p分配加法地址,因此p指向加法函数。
Hope this explanation clears your doubts.
希望这个解释清除你的疑虑。
Just like arrays, assigning a function name or address of the function to a pointer variable is the same. so your choice to place a &
before addition.
就像数组一样,将函数的函数名称或地址赋给指针变量也是一样的。所以你选择在添加之前放置一个&。
#2
1
In C, using the name of a function in a value context implicitly gives the address of that function. There is nothing else that it could give, so this is a convenience. But using an explicit &
in front also works, as you have discovered. The result is the same either way.
在C中,使用值上下文中的函数名称隐式地给出该函数的地址。它没有别的东西可以给,所以这是一个方便。但正如您所发现的那样,使用明确的&在前面也是有效的。结果是相同的两种方式。
#3
0
The name of a function is not a pointer. What you're seeing is a result of how expressions work in C.
函数的名称不是指针。您所看到的是表达式如何在C中工作的结果。
However, when used in an expression like
但是,在表达式中使用时
p = addition;
the name of the function is implicitly converted to a pointer. In this context, p = addition
(which implicitly converts addition
to a pointer, and assigns the result to p
) and p = &addition
(which computes a pointer with the same value, and assigns the result to p
) have the same net effect.
函数的名称隐式转换为指针。在此上下文中,p =加法(隐式地将加法转换为指针,并将结果赋值给p)和p =&addition(计算具有相同值的指针,并将结果赋值给p)具有相同的净效果。
#4
0
Function names are pointer-like and function pointers are function-like:
函数名称是指针式的,函数指针是函数式的:
#include <stdio.h>
void hw(void) { puts("hello world"); }
int main()
{
/*all three work*/
hw();
(&hw)();
(*&hw)();
}
That makes them a little bit like arrays in that they autoconvert to pointers and that you can't really get to the "value" of a function (a block of op codes), which is because C doesn't have any operations on it.
这使得它们有点像数组,因为它们会自动转换为指针并且你无法真正达到函数的“值”(操作码块),这是因为C对它没有任何操作。
The difference between functions and function pointers is blurred in most contexts in C, but it does come up if you typedef
.
在C语言的大多数情况下,函数和函数指针之间的区别都很模糊,但是如果你输入了type,它确实会出现。
#include <stdio.h>
typedef void void_void_t(void); //a function typedef
typedef void (*void_void_pt)(void); //a function pointer typedef
void_void_t hw; //no storage, only verifies that hw matches the signature
void_void_pt hwp; //a null pointer of the void_void_pt type; takes up memory
void hw(void) { puts("hello world"); }
#1
2
int (*p)(int, int);
int(* p)(int,int);
Here you are declaring a pointer to a function.
在这里,您将声明一个指向函数的指针。
In p = addition;
You are pointing p
to addition
function. Just as variables which are declared have addresses, functions too have their own addresses. So, by doing that you are assigning the address of addition
to p
and thus p
points to addition
function.
在p =加法;您指向p添加功能。就像声明的变量具有地址一样,函数也有自己的地址。因此,通过这样做,您将为p分配加法地址,因此p指向加法函数。
Hope this explanation clears your doubts.
希望这个解释清除你的疑虑。
Just like arrays, assigning a function name or address of the function to a pointer variable is the same. so your choice to place a &
before addition.
就像数组一样,将函数的函数名称或地址赋给指针变量也是一样的。所以你选择在添加之前放置一个&。
#2
1
In C, using the name of a function in a value context implicitly gives the address of that function. There is nothing else that it could give, so this is a convenience. But using an explicit &
in front also works, as you have discovered. The result is the same either way.
在C中,使用值上下文中的函数名称隐式地给出该函数的地址。它没有别的东西可以给,所以这是一个方便。但正如您所发现的那样,使用明确的&在前面也是有效的。结果是相同的两种方式。
#3
0
The name of a function is not a pointer. What you're seeing is a result of how expressions work in C.
函数的名称不是指针。您所看到的是表达式如何在C中工作的结果。
However, when used in an expression like
但是,在表达式中使用时
p = addition;
the name of the function is implicitly converted to a pointer. In this context, p = addition
(which implicitly converts addition
to a pointer, and assigns the result to p
) and p = &addition
(which computes a pointer with the same value, and assigns the result to p
) have the same net effect.
函数的名称隐式转换为指针。在此上下文中,p =加法(隐式地将加法转换为指针,并将结果赋值给p)和p =&addition(计算具有相同值的指针,并将结果赋值给p)具有相同的净效果。
#4
0
Function names are pointer-like and function pointers are function-like:
函数名称是指针式的,函数指针是函数式的:
#include <stdio.h>
void hw(void) { puts("hello world"); }
int main()
{
/*all three work*/
hw();
(&hw)();
(*&hw)();
}
That makes them a little bit like arrays in that they autoconvert to pointers and that you can't really get to the "value" of a function (a block of op codes), which is because C doesn't have any operations on it.
这使得它们有点像数组,因为它们会自动转换为指针并且你无法真正达到函数的“值”(操作码块),这是因为C对它没有任何操作。
The difference between functions and function pointers is blurred in most contexts in C, but it does come up if you typedef
.
在C语言的大多数情况下,函数和函数指针之间的区别都很模糊,但是如果你输入了type,它确实会出现。
#include <stdio.h>
typedef void void_void_t(void); //a function typedef
typedef void (*void_void_pt)(void); //a function pointer typedef
void_void_t hw; //no storage, only verifies that hw matches the signature
void_void_pt hwp; //a null pointer of the void_void_pt type; takes up memory
void hw(void) { puts("hello world"); }