Thread: Call a CPP struct in c

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    7

    Call a CPP struct in c

    Hello everyone
    I have a cpp project which has a struct defined in header file as
    Code:
    namespace ZWApi {
       struct ZWNode {
            int typeBasic;
    	int typeGeneric;
    	int typeSpecific;
    	bool sleepingDevice;
    	int plutoDeviceTemplateConst;
    	int stateBasic;
    	time_t pingtime;
    	std::string associationList[4];
        };
    }
    I have a C file which needs this struct.i want to call ZWNode struct in c and create a struct called pNode.

    So far I have managed to link cpp and c files...Now i am trying to create the new struct pNode as

    struct ZWApi::ZWNode pNode;

    but i am having an error :error: expected identifier or ‘(’ before ‘:’ token
    struct ZWApi::ZWNode pNode;
    WHat am I doing wrong...I hope i made my problem clear..any suggestions appreciated...thankss

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    C knows nothing of bool or std::string.

    How to mix C and C++, C++ FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The scope resolution operator :: is C++ syntax: it cannot be used in a C program. Even if you declared the C++ struct to be in the global namespace, you may still have the problem of name mangling, hence the use of extern "C" for C linkage. The thing is though, you cannot write this in the header:
    Code:
    #ifdef __cplusplus
    extern "C"
    {
    #endif
        struct ZWNode
        {
            /* ... */
            std::string associationList[4];
        };
    #ifdef __cplusplus
    }
    #endif
    since if it gets included in the C source file, you once again have the problem of C++ syntax in a C compilation unit, and besides, you'll need to #include <string> in the header.

    I may be mistaken, but I suppose that you will have to write a C++ wrapper around ZWApi::ZWNode that has C linkage through extern "C" and uses no C++-specific constructs, e.g., because uses the opaque pointer idiom such that the interface is provided as a set of non-member non-friend functions with C linkage that accept the opaque pointer as an argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    Quote Originally Posted by Salem View Post
    C knows nothing of bool or std::string.

    How to mix C and C++, C++ FAQ
    Not true for bool. The data type 'bool' was added to the C99 Standard, along with 'true' == 1, and 'false' == 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to call a function that takes a STRUCT parameter
    By fouzimedfouni in forum C Programming
    Replies: 2
    Last Post: 02-24-2015, 10:24 AM
  2. Returning a struct from a function call
    By hzcodec in forum C Programming
    Replies: 4
    Last Post: 07-09-2012, 01:50 PM
  3. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  4. struct in function call
    By Jasper in forum C Programming
    Replies: 2
    Last Post: 09-09-2009, 02:18 AM
  5. Replies: 2
    Last Post: 05-04-2003, 05:30 AM