Calls to C from C++

The following example is a C function that is to be called from C++:

Example 17. Defining the function in C

struct S {
    int i;
void cfunc(struct S *p) {
/* the definition of the C function to be called from C++ */
    p->i += 5;

To call this C function from C++, declare it with extern "C":

Example 18. Calling a C function from C++

struct S {            // has no base classes 
                      // or virtual functions
    S(int s) : i(s) { }
    int i;
}; extern "C" void cfunc(S *); 
// declare the C function to be called from C++
int f(){
    S s(2);           // initialize 's'
    cfunc(&s);        // call 'cfunc' so it can change 's'
    return s.i * 3;

Show/hideSee also

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