[問題] c語言array裡放function
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
IAR 8051
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
no
問題(Question):
各位大大,小弟想請問一下,我是否能在array裡放function,如下:
#include <stdio.h>
#include <stdlib.h>
void test( void )
{
printf( "test\n" );
}
void test1( void )
{
printf( "test1\n" );
}
void test2( void )
{
printf( "test2\n" );
}
void ( *p[3] )( void );
int main()
{
p[0] = test;
p[1] = test1;
p[2] = test2;
int nu;
printf( "nu = " );
scanf( "%d", &nu );
p[nu];
return 0;
}
用一個array把test,test1,test2這些function放到array,使用者只要輸入nu
就用array[nu],去呼叫function出來.
主要是因為我會有很多swtich case,我想要用這個方式去取,不知這樣是否可以?
想請問各位大大,我那裡的想法有錯誤?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.217.230.190
※ 編輯: jimmycch 來自: 61.217.230.190 (01/04 22:31)
推
01/04 22:33, , 1F
01/04 22:33, 1F
→
01/04 22:33, , 2F
01/04 22:33, 2F
→
01/04 22:34, , 3F
01/04 22:34, 3F
→
01/04 23:56, , 4F
01/04 23:56, 4F
→
01/05 22:27, , 5F
01/05 22:27, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章