Out parameters in C

user1128265 picture user1128265 · Feb 4, 2012 · Viewed 29.6k times · Source
void swap(int &first, int &second){
    int temp = first;
    first = second;
    second = temp;
}
int a=3,b=2;
swap(a,b);

The compiler complaints that void swap(int &first, int &second) has a syntax error. Why? Doesn't C support references?

Answer

Sean picture Sean · Feb 4, 2012

C doesn't support passing by reference; that's a C++ feature. You'll have to pass pointers instead.

void swap(int *first, int *second){
    int temp = *first;
    *first = *second;
    *second = temp;
}

int a=3,b=2;
swap(&a,&b);