看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《walm2O (我是屁原~噗噗)》之銘言: : 在if condition中,類似這種形式 : scanf("%d",&index); : if( buf[index] == something ) : 就是由standard input決定buffer index做判斷 : 印象中這種程式碼似乎很少出現? : 有找過core utilities 等等地方都沒有 : 可能是我程式碼看過的太少.. : 所以問問大家有沒有在哪看過? : 感謝 ~ 我想到的是 function pointer array, 不過沒有用到 index 較常在做 menu 或一些 選擇性執行 時用到 #include <stdio.h> typedef void (*pFunc)(void); void sel1() {printf("you select one.\n");} void sel2() {printf("you select two.\n");} void sel3() {printf("you select three.\n");} void sel4() {printf("you select four.\n");} int main() { int select=0, Size; pFunc fptr[] = {sel1, sel2, sel3, sel4}; Size = sizeof(fptr) / sizeof(fptr[0]); do{ printf("input a integer:"); scanf("%d", &select); if(select>=0 && select<Size) fptr[select](); }while(select>=0 && select<Size); return 0; } -- YouLoveMe() ? LetItBe() : LetMeFree(); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.177.73.222
xatier:推一個 06/20 16:20
angleevil:<m.m> 06/20 17:45
walm2O:比較偏向找index沒規範的 不過還是很感謝XD 06/21 14:49