#define INLINE static inline __attribute__((always_inline))
INLINE void swap(int a, int b){
int tmp = a;
a = b;
b = tmp;
}
int main(){
int x = 10;
int y = 20;
swap(x, y);
printf("x:%d y:%d", x, y);
return 0;
}
output: x:10 y:20
If inline functions are insert to the function they are called, why does this function not give correct results?
You'll have to write it just like you would write an ordinary function. That is:
And leave it to the compiler from there. It will optimize out the indirect addressing with pointers as part of the inlining.
The function you wrote has no side effects and so the compiler will just regard it as one big no-op and remove it entirely.