In my C++ program, I need to use a data structure defined in a C library that I cannot modify. I will explain my question will a simplified example:
In test.h, a struct L1 is defined, with nested structures L2 and L3.
========== test.h BEGIN ===========
struct L1 {
int element;
struct L2 {
int element;
struct L3 {
int element;
} *L3;
} *L2;
};
========== test.h END =============
I think, the way that the data structure is defined is a bit nasty, but I simply cannot correct it. Now if I am using C, I will be able to define variables like
struct L2 *l2;
struct L3 *l3;
In C++, I tried various different methods, but I still cannot define a pointer to struct L2. The following is an attempt which does not compile. Any tips will be appreciated. Thanks a lot!
=============== test.cpp BEGIN ================
#include <iostream>
#include "test.h"
Seems that your solution works. But now if I define the variables in the way you suggested, and use these variables to call the C-functions in the library, I will get error like:
test.cpp:34: error: cannot convert ‘L1::L2::L3*’ to ‘L3*’ for argument ‘1’ to ‘void cfunction3(L3*)’
the C-library was compiled by gcc. The C++ file is compiled by g++. The function prototypes for the C-library are properly enclosed in
extern "C" { ...... }