top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I pass an object of a C++ class to/from a C function?

+6 votes
727 views

Please explain with an example

posted Apr 27, 2016 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Here's an example (for info on extern "C", see the previous two FAQs).

Fred.h:

    /* This header can be read by both C and C++ compilers */
    #ifndef FRED_H
    #define FRED_H

    #ifdef __cplusplus
      class Fred {
      public:
        Fred();
        void wilma(int);
      private:
        int a_;
      };
    #else
      typedef
        struct Fred
          Fred;
    #endif

    #ifdef __cplusplus
    extern "C" {
    #endif

    #if defined(__STDC__) || defined(__cplusplus)
      extern void c_function(Fred*);   /* ANSI C prototypes */
      extern Fred* cplusplus_callback_function(Fred*);
    #else
      extern void c_function();        /* K&R style */
      extern Fred* cplusplus_callback_function();
    #endif

    #ifdef __cplusplus
    }
    #endif

    #endif /*FRED_H*/

Fred.cpp:

  // This is C++ code

    #include "Fred.h"

    Fred::Fred() : a_(0) { }

    void Fred::wilma(int a) { }

    Fred* cplusplus_callback_function(Fred* fred)
    {
      fred->wilma(123);
      return fred;
    }

main.cpp:

 // This is C++ code

    #include "Fred.h"

    int main()
    {
      Fred fred;
      c_function(&fred);
      ...
    }

c-function.c:

   /* This is C code */

    #include "Fred.h"

    void c_function(Fred* fred)
    {
      cplusplus_callback_function(fred);
    }

Unlike your C++ code, your C code will not be able to tell that two pointers point at the same object unless the pointers are exactly the same type. For example, in C++ it is easy to check if a Derived* called dp points to the same object as is pointed to by a Base* called bp: just say if (dp == bp) .... The C++ compiler automatically converts both pointers to the same type, in this case to Base*, then compares them. Depending on the C++ compiler's implementation details, this conversion sometimes changes the bits of a pointer's value.
(Technical aside: Most C++ compilers use a binary object layout that causes this conversion to happen with multiple inheritance and/or virtual inheritance. However the C++ language does not impose that object layout so in principle a conversion could also happen even with non-virtual single inheritance.)

The point is simple: your C compiler will not know how to do that pointer conversion, so the conversion from Derived* to Base*, for example, must take place in code compiled with a C++ compiler, not in code compiled with a C compiler.
reference-https://isocpp.org/wiki/faq/mixing-c-and-cpp#cpp-objs-passed-to-c

answer Apr 27, 2016 by Devendra Bohre
Similar Questions
0 votes

Suppose I have been given an array and I need to findout all such combination where a^2 + b^2 = c^2 then how can I achieve that?

C/C++ code would be helpful?

+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

...