Is it possible to swap C functions?

后端 未结 5 456
难免孤独
难免孤独 2021-01-06 08:09

Looking to see if anyone knows if its possible to swap C functions...?

 void swap2(int(*a)(int), int(*b)(int)) {
   int(*temp)(int) = a;
   *a = *b;
   *b =          


        
5条回答
  •  不思量自难忘°
    2021-01-06 08:55

    Your code will give error like "invalid lvalue" at the time of assignment. As I can see in your code you are trying to swap pointers without changing its values so have a look on below solution.

    void swap2(int(**a)(int), int(**b)(int)) {   
       int(*temp)(int) = *a;
       *a = *b;
       *b = temp;
    }
    
    int main(){
        int(*temp1)(int) = &funcA;
        int(*temp2)(int) = &funcB;
        swap2(&temp1,&temp2);
    }
    

提交回复
热议问题