Note that there are some explanatory texts on larger screens.

plurals
  1. POtype conversion for struct of array from c++ to c
    primarykey
    data
    text
    <p>I have two libraries, from C++ and C, respectively:</p> <p>lib A in C++: </p> <pre><code>typedef struct { char val[4096]; } AString; </code></pre> <p>lib B in C:</p> <pre><code>typedef struct STRING_TYPE{ unsigned char *u1_list; signed int i4_length; } BString; </code></pre> <p>If I cannot touch lib A &amp; B code, i.e., cannot add a cast operator in AString class. How to convert from AString to BString?</p> <p><strong>EDIT 1</strong>: no separate function defined:</p> <pre><code>AString as=XXX; // given variable 'as' to convert, unsigned char p[4096]; size_t const N = std::min(strlen(as.val), 4096); BString bs = { p, N }; std::copy( static_cast&lt;unsigned char*&gt;(&amp;as.val[0]), static_cast&lt;unsigned char*&gt;(&amp;as.val[N]), bs.u1_list ); // or use memcpy like: memcpy (bs.u1_list, as.val, N+1); // then follows the part to use converted variable bs; func(bs); </code></pre> <p>but the code is not module structured.</p> <p>or else:</p> <p><strong>EDIT 2</strong>: which is essentially EDIT_1, define a function, </p> <pre><code>void convert (AString&amp; as, BString&amp; bs) { size_t const N = std::min(strlen(as.val), 4096); std::copy( static_cast&lt;unsigned char*&gt;(&amp;as.val[0]), static_cast&lt;unsigned char*&gt;(&amp;as.val[N]), bs.u1_list ); // or use memcpy like: memcpy (bs.u1_list, as.val, N+1); bs.i4_length = N; } </code></pre> <p>but before call this function, the user has to define an array, like</p> <pre><code>AString as=XXX; // given variable 'as' to convert, unsigned char p[4096]; BString bs; bs.u1_list = p; convert(as, bs); // then follows the part to use converted variable bs; func(bs); </code></pre> <p>Both are quite awkward. </p> <p>Isn't there any self-contained, separate-function way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload