Passing a reference between C and C++

To pass references between C and C++, use the extern "C" declaration:

Example 29. Defining the C++ function

extern "C" int cfunc(const int&); 
// Declaration of the C function to be called from C++ extern "C"
int cppfunc(const int& r) {
// Definition of the C++ function to be called from C.
    return 7 * r;
}
int f() {
    int i = 3;
    return cfunc(i);    // passes a pointer to 'i'
}

In the C function, declare the C++ reference with extern:

Example 30. Defining the C function

extern int cppfunc(const int*);    
/* declaration of the C++ function to be called from C */
int cfunc(const int *p) {       
/* definition of the C function to be called from C++ */
    int k = *p + 4;
    return cppfunc(&k);
}

Copyright © 2010-2012 ARM. All rights reserved.ARM DUI 0471G
Non-ConfidentialID021412