- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Sirs and Madams,
I would like to translate KBA84041, please confirm to my work.
Regards,
Masashi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, Masashi-san
Confirm to work this KBA.
Thanks
Jenna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jenna-san,
I translated KBA84041 into Japanese.
We would appreciate it if you could confirm.
Regards,
Masashi
/***************************************************************************/
関数ポインタ - KBA84041
userc_45255 が 2012/12/07 22:46 に作成。userc_45255 が 2015/12/07 15:06 に変更。
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などのすべてのマイクロコントローラーが関数ポインタをサポートします。