Note that there are some explanatory texts on larger screens.

plurals
  1. POTypecasting (or deference) void * to struct foo*
    primarykey
    data
    text
    <p>In api.h</p> <pre><code>typedef void* hidden_my_type; void do_something(my_type x); </code></pre> <p>In core.c</p> <pre><code>struct _my_type { int a; } void do_something(hidden_my_type void_x) { struct *_my_type x = void_x; /*Don't understand is that correct way to do, as I'm getting segmentation fault error */ printf("Value: %d\n", x-&gt;a); } </code></pre> <p>Other way I thought as,</p> <pre><code>struct *_my_type x = (struct _my_type *)malloc(sizeof(struct _my_type)); void_x = x printf(Value: %d\n", x-&gt;a); </code></pre> <p>But still I'm getting seg-fault error.</p> <hr> <p>ok here is the problem with void*....</p> <p>e.g. in core.c</p> <pre><code>void init_my_type(hidden_my_type a) { my_type *the_a = malloc(...); a = the_a // &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; is this correct?! a is void* and the_a // is original type pthread_cond_init(&amp;the_a-&gt;...); .. (in short any other methods for init ..) } void my_type_destroy(my_hidden_type x) { my_type *the_x = x; pthread_detroy(&amp;the_x-&gt; ...); } </code></pre> <p>in main.c</p> <pre><code>test() { my_hidden_type x; init_my_type(x); .... my_type_detroy(x); } </code></pre> <p>this it self should fail. as in main.c test function, x is void* ... init will allocate but in destroy I'm again passing void* .. which can be anything!</p> <p>EDIT (Solved for me)</p> <p>In api.h</p> <pre><code>typedef void* hidden_my_type; void do_something(my_type x); </code></pre> <p>In core.c</p> <pre><code> struct _my_type { int a; } void init_hidden_type(hidden_my_type void_p_my_type) { struct _my_type *real_my_type = (struct _my_type *)malloc(sizeof(struct _my_type)); //--- Do init for your type --- void_p_my_type = real_my_type; } void do_something(hidden_my_type void_x) { struct *_my_type x = void_x; printf("Value: %d\n", x-&gt;a); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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