[英]Unable to understand pointers in c in a function
I was reading this code on a website.我正在网站上阅读此代码。 I am fairly new to programming so please explain in a bit more detail.我对编程相当陌生,所以请更详细地解释一下。
#include <stdio.h>
// A normal function with an int parameter
// and void return type
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;
/* The above line is equivalent of following two
void (*fun_ptr)(int);
fun_ptr = &fun;
*/
// Invoking fun() using fun_ptr
(*fun_ptr)(10);
return 0;
}
Doubts-怀疑——
I am not able to understand this type of declaration and assignment void (*fun_ptr)(int) = &fun;
我无法理解这种类型的声明和赋值void (*fun_ptr)(int) = &fun;
I mean that if we declare a data type, then we do it like int a;
我的意思是,如果我们声明一个数据类型,那么我们就像int a;
and assign it as a=10;
并将其分配为a=10;
but here we are assigning it by writing (*fun_ptr)(10);
但在这里我们通过写(*fun_ptr)(10);
. . Kindly help.请帮忙。
Instead of this record而不是这个记录
(*fun_ptr)(10);
you could just write你可以写
fun_ptr(10);
That is it is a function call of the function fun
pointed to by the function pointer fun_ptr
due to the initialization of that pointer in its declaration by the function address That is it is a function call of the function fun
pointed to by the function pointer fun_ptr
due to the initialization of that pointer in its declaration by the function address
void (*fun_ptr)(int) = &fun;
In turn this declaration could be written simpler like反过来,这个声明可以写得更简单,比如
void (*fun_ptr)(int) = fun;
because a function designator (in this case fun
) used in expressions as for example an initializer is implicitly converted to pointer to the function.因为在表达式中使用的 function 指示符(在本例中为fun
),例如初始化器被隐式转换为指向 function 的指针。
You could use a typedef alias for the function type the following way您可以通过以下方式为 function 类型使用 typedef 别名
typedef void Func( int );
In this case the above declaration of the function pointer could look simpler like在这种情况下,function 指针的上述声明可能看起来更简单,例如
Func *fun_ptr = fun;
Here is your program rewritten using a typedef for the function type of the function fun
.这是使用 function fun
的 function 类型的 typedef 重写的程序。
#include <stdio.h>
typedef void Func( int );
// Function declaration without its definition using the typedef
// This declaration is redundant and used only to demonstrate
// how a function can be declared using a typedef name
Func fun;
// Function definition. In this case you may not use the typedef name
void fun( int a )
{
printf("Value of a is %d\n", a);
}
int main(void)
{
// Declaration of a pointer to function
Func *fun_ptr = fun;
// Call of a function using a pointer to it
fun_ptr( 10 );
return 0;
}
Lets rewrite it a little bit, by using type-aliases and some comments:让我们通过使用类型别名和一些注释来稍微重写一下:
// Define a type-alias names fun_pointer_type
// This type-alias is defined as a pointer (with the asterisk *) to a function,
// the function takes one int argument and returns no value (void)
typedef void (*fun_pointer_type)(int);
// Use the type-alias to define a variable, and initialize the variable
// This defines the variable fun_ptr being the type fun_pointer_type
// I.e. fun_ptr is a pointer to a function
// Initialize it to make it point to the function fun
fun_pointer_type fun_ptr = &fun;
// Now *call* the function using the function pointer
// First dereference the pointer, to get the function it points to
// Then call the function, passing the single argument 10
(*fun_ptr)(10);
Hopefully it makes things a little clearer what's going on.希望它能让事情变得更清楚。
Following is the meaning of two statments.以下是两句话的意思。
void (*fun_ptr)(int) = &fun;
this is called declaring and initializing the fun_ptr
in the same line, this is same as doing int a = 10;
这称为在同一行中声明和初始化fun_ptr
,这与执行int a = 10;
相同。
(*fun_ptr)(10);
is not assignment statement, it is invoking the function fun
through function pointer fun_ptr
.不是赋值语句,它是通过 function 指针fun_ptr
调用 function fun
。
you can also use typedef
to create a new user defined out of function pointer and use as shown in above answer.您还可以使用typedef
创建一个使用 function 指针定义的新用户,并如上答案所示使用。
This is an advanced topic if you are new to programming, fun_ptr
is a pointer to a function.如果您是编程新手,这是一个高级主题, fun_ptr
是指向 function 的指针。
The declaration:声明:
void (*fun_ptr)(int) = fun;
Means fun_ptr
is a pointer to a function taking an int
argument and returning void
, initialize if with a pointer to the function fun
.意味着fun_ptr
是指向 function 的指针,采用int
参数并返回void
,如果使用指向 function fun
的指针进行初始化。 (you don't need the &
) (你不需要&
)
The line:该行:
(*fun_ptr)(10);
does not assign anything, it calls the function pointed to by fun_ptr
, but is way to complex没有分配任何东西,它调用了 fun_ptr 指向的fun_ptr
,但是很复杂
fun_ptr(10);
As fun_ptr
points to fun
this is equivalant to `fun(10).由于fun_ptr
指向fun
,这等同于 `fun(10)。
Using a function pointer has its use eg in a sort function where the comparison function is passed in as a function pointer so the sorting can be different between calls. Using a function pointer has its use eg in a sort function where the comparison function is passed in as a function pointer so the sorting can be different between calls. achieves the same thing and is much easier on the eyes.达到同样的效果,而且在眼睛上更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.