簡體   English   中英

無法理解 function 中 c 中的指針

[英]Unable to understand pointers in c in a function

我正在網站上閱讀此代碼。 我對編程相當陌生,所以請更詳細地解釋一下。

#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; 
} 

懷疑——
我無法理解這種類型的聲明和賦值void (*fun_ptr)(int) = &fun;
我的意思是,如果我們聲明一個數據類型,那么我們就像int a; 並將其分配為a=10; 但在這里我們通過寫(*fun_ptr)(10); . 請幫忙。

而不是這個記錄

(*fun_ptr)(10);

你可以寫

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

void (*fun_ptr)(int) = &fun;

反過來,這個聲明可以寫得更簡單,比如

void (*fun_ptr)(int) = fun;

因為在表達式中使用的 function 指示符(在本例中為fun ),例如初始化器被隱式轉換為指向 function 的指針。

您可以通過以下方式為 function 類型使用 typedef 別名

typedef void Func( int );

在這種情況下,function 指針的上述聲明可能看起來更簡單,例如

Func *fun_ptr = 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;
}

讓我們通過使用類型別名和一些注釋來稍微重寫一下:

// 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);

希望它能讓事情變得更清楚。

以下是兩句話的意思。

void (*fun_ptr)(int) = &fun; 這稱為在同一行中聲明和初始化fun_ptr ,這與執行int a = 10;相同。

(*fun_ptr)(10); 不是賦值語句,它是通過 function 指針fun_ptr調用 function fun

您還可以使用typedef創建一個使用 function 指針定義的新用戶,並如上答案所示使用。

如果您是編程新手,這是一個高級主題, fun_ptr是指向 function 的指針。

聲明:

void (*fun_ptr)(int) = fun;

意味着fun_ptr是指向 function 的指針,采用int參數並返回void ,如果使用指向 function fun的指針進行初始化。 (你不需要&

該行:

(*fun_ptr)(10);

沒有分配任何東西,它調用了 fun_ptr 指向的fun_ptr ,但是很復雜

fun_ptr(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. 達到同樣的效果,而且在眼睛上更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM