简书链接:c语言函数回调函数与指针取值
文章字数:226,阅读全文大约需要1分钟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| //#include <processenv.h>
void test1();
void testEnum();
void valuePoiner();
void funcPointer();
void callFuncPointer(void*(*callback)(int)); int callbackMethod(int arg);
extern void write_extern() { printf(" write_extern"); }; enum WEEK { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
enum PLAYMODE { LOOP_PLAY, ORDER_PLAY, SINGLE_LOOP, LIST_PLAY, RANDOM_PLAY };
int main() {
valuePoiner(); funcPointer(); callFuncPointer(callbackMethod); // testEnum();
// test1();
}
void callFuncPointer(void* (*callback)(int)) {//*在某个值的左边表示取值,在右边表示是声明指针。
int values[]={6,58,7,1,15}; int index; for(index=0;index<5;index++){
printf("指针方式取值 %d\n",*(values+index)); // printf("指针方式取值 %d\n",*(values++));//会修改指针,不太推荐使用 printf("错误的指针方式取值 %d\n",*(values)+index);//这种方式取值错误,还是基于index的值 的递增而不是指针的递增。 printf("通过数组访问值:%d\n",values[index]); // =(*(value)+index)+callback(index); printf("current idnex\n %d",index); // *(values+index)=1;//修改数组元素 // printf("修改之后:%d\n",*(values+index)); *(values+index)= *(values+index)+callback(index); printf("调用之后的值:%d\n",*(values+index)); printf("\n\t\t\n");
}
// callFuncPointer(2);
}
int callbackMethod(int value) { int randomvalue=rand()%100+1;//1-100随机 printf("[回调调用]随机值: %d,传递过来的值%d\n ",randomvalue,value); return randomvalue+value;
}
|