Communitiy Transration - Function Pointer - KBA84041

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
MiNe_85951
Level 7
Level 7
Distributor - TED (Japan)
50 likes received 500 replies posted 50 solutions authored

Dear Sirs and Madams,

I would like to translate KBA84041, please confirm to my work.

Regards,
Masashi

0 Likes
2 Replies
JennaJo
Moderator
Moderator
Moderator
1000 replies posted 750 replies posted 500 replies posted

Hi, Masashi-san

Confirm to work this KBA.

Thanks

Jenna

Jenna Jo
0 Likes
MiNe_85951
Level 7
Level 7
Distributor - TED (Japan)
50 likes received 500 replies posted 50 solutions authored

Jenna-san,

I translated KBA84041 into Japanese.

We would appreciate it if you could confirm.

Regards,

Masashi

/***************************************************************************/

関数ポインタ - KBA84041

Version: *A

質問:

関数へのポインタを宣言するにはどのようすればよいですか?

回答:

関数ポインタ:関数ポインタは、後でその関数ポインタを介して呼び出すことができる関数のアドレスを格納する変数です。

ポインタの値を変更することで、同じプロジェクト内の異なる関数を呼び出すことができます。

このため、関数ポインタのプロトタイプと、ポインタを使用して呼び出される関数は同じでなければなりません。

関数ポインタは次のように宣言できます。

<関数の戻り値の型>(* <ポインタの名前>)(関数の引数の型)

例:

void (*fptr) (int);

ここで、fptrは1つの引数(整数)を取る関数へのポインタであり、voidを返します。

関数ポインタが指す関数を呼び出すには、関数ポインタを呼び出す関数の名前であるかのように扱います。

#include<stdio.h>

void my_func1(int x)
{
          //print the function
}
void my_func2(int y)
{
          //print the function
}
void main()
{

void (*fptr) (int);

fptr=&my_func1;      //& is optional
(*fptr)(2);                  //calling the function my_func1 through pointer

fptr=&my_func2;      //& is optional
(*fptr)(6);                  //calling the function my_func2 through pointer

}

関数ポインタはCのプロパティです。

そのため、PSoC1、PSoC3、PSoC4、PSoC5などのすべてのマイクロコントローラーが関数ポインタをサポートします。

0 Likes